<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="format-detection" content="telephone=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><link rel="icon" href="/blog/blog/images/icons/favicon-16x16.png?v=2.6.2" type="image/png" sizes="16x16"><link rel="icon" href="/blog/blog/images/icons/favicon-32x32.png?v=2.6.2" type="image/png" sizes="32x32"><meta name="description" content=".back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;} .back-top&gt;a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height:">
<meta property="og:type" content="article">
<meta property="og:title" content="VUE学习笔记-4-VUE小技巧">
<meta property="og:url" content="https://bruce-jj.gitee.io/blog/2021/05/12/VUE%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95%E5%8F%8A%E7%9B%B8%E5%BA%94%E5%BA%94%E7%94%A8-4/index.html">
<meta property="og:site_name" content="斯人若彩虹 遇上方知有">
<meta property="og:description" content=".back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;} .back-top&gt;a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height:">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20210416123833199.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/2021041612451596.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20210514102019792.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70">
<meta property="article:published_time" content="2021-05-12T03:18:03.000Z">
<meta property="article:modified_time" content="2021-05-19T15:17:05.056Z">
<meta property="article:author" content="何水浪">
<meta property="article:tag" content="前端框架学习">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/20210416123833199.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70"><title>VUE学习笔记-4-VUE小技巧 | 斯人若彩虹 遇上方知有</title><link ref="canonical" href="https://bruce-jj.gitee.io/blog/2021/05/12/VUE%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95%E5%8F%8A%E7%9B%B8%E5%BA%94%E5%BA%94%E7%94%A8-4/"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.12.1/css/all.min.css" type="text/css"><link rel="stylesheet" href="/blog/css/index.css?v=2.6.2"><script>var Stun = window.Stun || {};
var CONFIG = {
  root: '/blog/',
  algolia: undefined,
  assistSearch: undefined,
  fontIcon: {"prompt":{"success":"fas fa-check-circle","info":"fas fa-arrow-circle-right","warning":"fas fa-exclamation-circle","error":"fas fa-times-circle"},"copyBtn":"fas fa-copy"},
  sidebar: {"offsetTop":"20px","tocMaxDepth":6},
  header: {"enable":true,"showOnPost":true,"scrollDownIcon":false},
  postWidget: {"endText":true},
  nightMode: {"enable":true},
  back2top: {"enable":true},
  codeblock: {"style":"default","highlight":"light","wordWrap":false},
  reward: false,
  fancybox: false,
  zoomImage: {"gapAside":"20px"},
  galleryWaterfall: undefined,
  lazyload: false,
  pjax: undefined,
  externalLink: {"icon":{"enable":true,"name":"fas fa-external-link-alt"}},
  shortcuts: undefined,
  prompt: {"copyButton":"复制","copySuccess":"复制成功","copyError":"复制失败"},
  sourcePath: {"js":"js","css":"css","images":"images"},
};

window.CONFIG = CONFIG;</script><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/blog/atom.xml" title="斯人若彩虹 遇上方知有" type="application/atom+xml">
</head><body><div class="container" id="container"><header class="header" id="header"><div class="header-inner"><nav class="header-nav header-nav--fixed"><div class="header-nav-inner"><div class="header-nav-menubtn"><i class="fas fa-bars"></i></div><div class="header-nav-menu"><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/"><span class="header-nav-menu-item__icon"><i class="fas fa-home"></i></span><span class="header-nav-menu-item__text">首页</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/archives/"><span class="header-nav-menu-item__icon"><i class="fas fa-folder-open"></i></span><span class="header-nav-menu-item__text">归档</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/categories/"><span class="header-nav-menu-item__icon"><i class="fas fa-layer-group"></i></span><span class="header-nav-menu-item__text">分类</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/tags/"><span class="header-nav-menu-item__icon"><i class="fas fa-tags"></i></span><span class="header-nav-menu-item__text">标签</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/friends/"><span class="header-nav-menu-item__icon"><i class="fas fa-link"></i></span><span class="header-nav-menu-item__text">友链</span></a></div></div><div class="header-nav-search"><span class="header-nav-search__icon"><i class="fas fa-search"></i></span><span class="header-nav-search__text">搜索</span></div><div class="header-nav-mode"><div class="mode"><div class="mode-track"><span class="mode-track-moon"></span><span class="mode-track-sun"></span></div><div class="mode-thumb"></div></div></div></div></nav><div class="header-banner"><div class="header-banner-info"><div class="header-banner-info__title">斯人若彩虹 遇上方知有</div><div class="header-banner-info__subtitle"></div></div></div></div></header><main class="main" id="main"><div class="main-inner"><div class="content-wrap" id="content-wrap"><div class="content" id="content"><!-- Just used to judge whether it is an article page--><div id="is-post"></div><div class="post"><header class="post-header"><h1 class="post-title">VUE学习笔记-4-VUE小技巧</h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-05-12</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-05-19</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">6.4k</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">53分</span></span></div></header><div class="post-body"><!-- 页面必备样式 -->
<style>
.back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;}
.back-top>a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height: 50px;border-radius: 50px;background-color:rgba(255,255,255,0.5);}
</style>
<div class="back-top">
    <a href="#top"> >返回顶部< </a>
</div>
<!-- toc -->
<!-- 必备样式结束 -->


        <h1 id="axios请求-包括Get请求与Post请求（注意是异步）"   >
          <a href="#axios请求-包括Get请求与Post请求（注意是异步）" class="heading-link"><i class="fas fa-link"></i></a><a href="#axios请求-包括Get请求与Post请求（注意是异步）" class="headerlink" title="axios请求 包括Get请求与Post请求（注意是异步）"></a>axios请求 包括Get请求与Post请求（注意是异步）</h1>
      <p>因为该方法是异步，<strong>因此无法准确知道元素的执行顺序。</strong></p>
<p>有可能axios方法后面的方法会提前执行，也有可能会延后执行。</p>

        <h2 id="Get方法"   >
          <a href="#Get方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#Get方法" class="headerlink" title="Get方法"></a>Get方法</h2>
      <figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> _this = <span class="built_in">this</span></span><br><span class="line"><span class="keyword">var</span> _data = <span class="built_in">this</span>.$data</span><br><span class="line">axios.get(main.requestAddress + <span class="string">&#x27;/book/showAllBooks&#x27;</span>, &#123;</span><br><span class="line">		<span class="attr">params</span>: &#123;<span class="string">&#x27;pn&#x27;</span>: _data.queryParams.currentPageNum&#125;</span><br><span class="line">	&#125;).then(<span class="function"><span class="keyword">function</span> (<span class="params">response</span>) </span>&#123;</span><br><span class="line">		<span class="comment">//从后端返回给前端的内容(code、message、data...等等)</span></span><br><span class="line">	&#125;).catch(<span class="function"><span class="keyword">function</span> (<span class="params">error</span>) </span>&#123;</span><br><span class="line">		</span><br><span class="line">	&#125;);</span><br></pre></td></tr></table></div></figure>

<p><strong>params:{ ‘data’: data}</strong> 是指以键值对的方式 往后台传输数据</p>
<p><strong>因此，后台需要用@RequestParam()标签</strong></p>
<p><strong>注意，在main.js中定义一个变量 var requestAddress=”192.xxx.xxx.xxx”</strong></p>
<p>同时<strong>导出这个main.js</strong></p>
<p>最后使用的时候<strong>记得 import main from ‘main.js的路径’</strong></p>

        <h3 id="请务必注意！"   >
          <a href="#请务必注意！" class="heading-link"><i class="fas fa-link"></i></a><a href="#请务必注意！" class="headerlink" title="请务必注意！"></a>请务必注意！</h3>
      <p><strong>使用axios请求时，如果需要用到在回调函数中用到该网页的内容。</strong></p>
<p><strong>必须在axios.get/post方法外 定义一个</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> _this = <span class="built_in">this</span></span><br></pre></td></tr></table></div></figure>

<p>注意外面的this与回调函数的this不一样！！！</p>

        <h2 id="Post方法"   >
          <a href="#Post方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#Post方法" class="headerlink" title="Post方法"></a>Post方法</h2>
      <p>POST方法比较复杂，涉及到<strong>数据绑定</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">&lt;el-form :rules=<span class="string">&quot;loginFormRules&quot;</span> ref=<span class="string">&quot;loginForm&quot;</span> </span><br><span class="line">		:model=<span class="string">&quot;loginForm&quot;</span> label-position=<span class="string">&quot;right&quot;</span> </span><br><span class="line">		label-width=<span class="string">&quot;auto&quot;</span> show-message&gt;</span><br><span class="line">		</span><br><span class="line">	<span class="xml"><span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;login-title&quot;</span>&gt;</span>欢迎登录<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span></span><br><span class="line">	<span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">style</span>=<span class="string">&quot;margin-top: 5px&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">		</span><br><span class="line">	<span class="xml"><span class="tag">&lt;<span class="name">el-form-item</span> <span class="attr">label</span>=<span class="string">&quot;用户名&quot;</span> <span class="attr">prop</span>=<span class="string">&quot;username&quot;</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;<span class="name">el-col</span> <span class="attr">:span</span>=<span class="string">&quot;22&quot;</span>&gt;</span></span></span><br><span class="line"><span class="xml">			<span class="tag">&lt;<span class="name">el-input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">v-model</span>=<span class="string">&quot;loginForm.username&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">el-input</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;/<span class="name">el-col</span>&gt;</span></span></span><br><span class="line"><span class="xml">	<span class="tag">&lt;/<span class="name">el-form-item</span>&gt;</span></span></span><br><span class="line">	</span><br><span class="line">	<span class="xml"><span class="tag">&lt;<span class="name">el-form-item</span> <span class="attr">label</span>=<span class="string">&quot;密码&quot;</span> <span class="attr">prop</span>=<span class="string">&quot;password&quot;</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;<span class="name">el-col</span> <span class="attr">:span</span>=<span class="string">&quot;22&quot;</span>&gt;</span></span></span><br><span class="line"><span class="xml">			<span class="tag">&lt;<span class="name">el-input</span> <span class="attr">type</span>=<span class="string">&quot;password&quot;</span> <span class="attr">v-model</span>=<span class="string">&quot;loginForm.password&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">el-input</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;/<span class="name">el-col</span>&gt;</span></span></span><br><span class="line"><span class="xml">	<span class="tag">&lt;/<span class="name">el-form-item</span>&gt;</span></span></span><br><span class="line">	</span><br><span class="line">	<span class="xml"><span class="tag">&lt;<span class="name">el-form-item</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;<span class="name">el-button</span> <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;loginSubmit(&#x27;loginForm&#x27;)&quot;</span>&gt;</span>登录<span class="tag">&lt;/<span class="name">el-button</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;<span class="name">el-button</span> <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;dialogVisible=true&quot;</span>&gt;</span>注册<span class="tag">&lt;/<span class="name">el-button</span>&gt;</span></span></span><br><span class="line"><span class="xml">	<span class="tag">&lt;/<span class="name">el-form-item</span>&gt;</span></span></span><br><span class="line">			</span><br><span class="line">&lt;/el-form&gt;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>

<p><strong>ref=”loginForm” 与 :model=”loginForm” 都是指绑定data中的loginForm</strong></p>
<p><strong>rules=”loginFormRules” 指的是绑定自定义规则</strong></p>
<p><strong>v-model=”loginForm.username”指的是 该值双向绑定loginForm里面的username属性</strong>因此，一定记得<strong>prop=loginForm.prop</strong></p>
<p>点击登录按钮时，<strong>loginForm</strong>作为参数传进去！</p>
<p><strong>1. loginForm里面的参数-&gt;对应的所有prop</strong></p>
<p>写在<strong>return</strong>里面即可：</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">loginForm: &#123;</span><br><span class="line">    <span class="attr">username</span>: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">    <span class="attr">password</span>: <span class="string">&#x27;&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p><strong>2. loginForm对应的规则，也写在return里面</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">loginFormRules: &#123;</span><br><span class="line">    <span class="attr">username</span>:[</span><br><span class="line">                &#123;<span class="attr">required</span>: <span class="literal">true</span>, <span class="attr">message</span>: <span class="string">&#x27;账号不可为空&#x27;</span>, <span class="attr">trigger</span>: <span class="string">&#x27;blur&#x27;</span>&#125;</span><br><span class="line">            ],</span><br><span class="line">    <span class="attr">password</span>: [</span><br><span class="line">                &#123;<span class="attr">required</span>: <span class="literal">true</span>, <span class="attr">message</span>: <span class="string">&#x27;密码不可为空&#x27;</span>, <span class="attr">trigger</span>: <span class="string">&#x27;blur&#x27;</span>&#125;</span><br><span class="line">            ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p><strong>require表示必须填，触发blur后，会提示message！</strong></p>
<p>这样，在JS中，可以将该表单<strong>loginForm</strong>与它对应的<strong>参数输入规则</strong>绑定在一起验证！</p>
<p>同时也可以自定义验证规则！<br><strong>在data(){这里} 中写</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> validateUserName = <span class="function">(<span class="params">rule, value, callback</span>) =&gt;</span> &#123;</span><br><span class="line">	<span class="keyword">if</span>(value === <span class="number">0</span>)&#123;</span><br><span class="line">		callback(<span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&#x27;这里是错误提示&#x27;</span>))	</span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		callback() <span class="comment">//验证成功，通过自定义规则！</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
<p>其中value是对应输入框的值！</p>
<p>callback用来回调</p>
<p><strong>提示对方错误信息</strong> </p>
<p>还是 </p>
<p><strong>让对方通过</strong></p>
<p>这样的话，可以在规则中，<strong>声明走自定义的规则方法：</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">loginFormRules: &#123;</span><br><span class="line">    <span class="attr">username</span>:[</span><br><span class="line">                &#123;<span class="attr">required</span>: <span class="literal">true</span>, <span class="attr">validator</span>: validateUserName,, trigger: <span class="string">&#x27;blur&#x27;</span>&#125;</span><br><span class="line">            ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p><strong>其他自定义规则 看官方文档</strong></p>
<p>具体登录+验证方法如下：</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">loginSubmit</span>(<span class="params">formName</span>)</span> &#123;</span><br><span class="line">	<span class="keyword">let</span> _this = <span class="built_in">this</span></span><br><span class="line">	<span class="comment">// 为表单绑定验证功能</span></span><br><span class="line">	<span class="built_in">this</span>.$refs[formName].validate(<span class="function">(<span class="params">valid</span>) =&gt;</span> &#123;</span><br><span class="line">		<span class="keyword">if</span> (valid) &#123;</span><br><span class="line">			<span class="comment">// 使用 vue-router 路由到指定页面，该方式称之为编程式导航</span></span><br><span class="line">			<span class="built_in">this</span>.$axios.post(main.requestAddress + <span class="string">`/user/login`</span>, _this.loginForm)</span><br><span class="line">				.then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">					_this.$data.message = res.data.message</span><br><span class="line">						<span class="comment">//若数据库查询失败 则返回重新登录信息</span></span><br><span class="line">						<span class="keyword">if</span> (res.data.data === <span class="literal">null</span>) &#123;</span><br><span class="line">							_this.openLoginFailMessage();</span><br><span class="line">							<span class="keyword">return</span>;</span><br><span class="line">						&#125;</span><br><span class="line">						sessionStorage.setItem(<span class="string">&quot;token&quot;</span>, <span class="literal">true</span>);</span><br><span class="line">						sessionStorage.setItem(<span class="string">&quot;username&quot;</span>, res.data.data.username);</span><br><span class="line">						_this.$router.push(<span class="string">&quot;/index&quot;</span>);<span class="comment">//路由跳转至 path=&gt; /index 具体看自己的路由配置</span></span><br><span class="line">					&#125;)</span><br><span class="line">					.catch(<span class="function"><span class="params">Error</span> =&gt;</span> &#123;</span><br><span class="line">						<span class="built_in">console</span>.log(<span class="built_in">Error</span>)</span><br><span class="line">					&#125;)</span><br><span class="line">			&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">				<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;);</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></div></figure>



        <h1 id="全局路由配置（基于session的前端权限）"   >
          <a href="#全局路由配置（基于session的前端权限）" class="heading-link"><i class="fas fa-link"></i></a><a href="#全局路由配置（基于session的前端权限）" class="headerlink" title="全局路由配置（基于session的前端权限）"></a>全局路由配置（基于session的前端权限）</h1>
      <p>1.首先搞清楚，除了登录、404..页面，其他的涉及到<strong>必须要先登录的业务数据页面</strong>都是要授权认证的！</p>
<p>2.授权认证，必须要经过<strong>成功登录</strong></p>
<p>3.每次页面进行跳转都要经过权限认证过程！</p>

        <h2 id="配置路由，除了登录页面，其他都要认证"   >
          <a href="#配置路由，除了登录页面，其他都要认证" class="heading-link"><i class="fas fa-link"></i></a><a href="#配置路由，除了登录页面，其他都要认证" class="headerlink" title="配置路由，除了登录页面，其他都要认证"></a>配置路由，除了登录页面，其他都要认证</h2>
      <p><strong>我们给相应涉及业务数据的页面，比如主页面index，加上需要拦截的头信息：</strong></p>
<p><strong>注意！登录页面是不需要加的！</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="attr">meta</span>: &#123;</span><br><span class="line">		<span class="attr">requireAuth</span>: <span class="literal">true</span> <span class="comment">//如果来该页面 需要验证</span></span><br><span class="line">	&#125;,</span><br><span class="line">	<span class="attr">path</span>: <span class="string">&#x27;/index&#x27;</span>,</span><br><span class="line">	<span class="attr">name</span>: <span class="string">&#x27;业务逻辑&#x27;</span>,</span><br><span class="line">	<span class="attr">component</span>: <span class="function">()=&gt;</span><span class="keyword">import</span>(<span class="string">&#x27;../views/ShowAll&#x27;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>然后，在页面拦截时，我们需要判断<strong>要去的页面是否需要授权？</strong></p>
<p><strong>也就是requireAuth是否是’true’？</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//添加全局路由拦截</span></span><br><span class="line">router.beforeEach(<span class="function">(<span class="params">to, <span class="keyword">from</span>, next</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (to.meta.requireAuth) &#123;</span><br><span class="line">        <span class="comment">//如果去的页面需要认证 那么就开始认证</span></span><br><span class="line">        <span class="keyword">if</span> (sessionStorage.getItem(<span class="string">&quot;token&quot;</span>) == <span class="string">&#x27;true&#x27;</span>) &#123;</span><br><span class="line">            next(); <span class="comment">//放行</span></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            alert(<span class="string">&#x27;请您登陆...&#x27;</span>);</span><br><span class="line">            <span class="comment">//否则下一步也放行 但放行到登录页面</span></span><br><span class="line">            next(&#123;</span><br><span class="line">                <span class="attr">path</span>: <span class="string">&#x27;/login&#x27;</span></span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123; <span class="comment">//如果不需要授权 比如直接去登录页面</span></span><br><span class="line">        <span class="keyword">if</span> (sessionStorage.getItem(<span class="string">&quot;token&quot;</span>) == <span class="string">&#x27;true&#x27;</span> &amp;&amp; to.path != <span class="string">&#x27;/login&#x27;</span>) &#123;</span><br><span class="line">            next(<span class="string">&#x27;/index&#x27;</span>)</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            next()</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></div></figure>

<ol>
<li><p>注意这里的<strong>sessionStorage.getItem(“token”)<strong>取出来的值是</strong>字符串类型</strong></p>
</li>
<li><p><strong>这里的 &amp;&amp; to.path!=’/login’ 是用户从index主界面 到login登录页面 而不想已经验证但还是跳到index页面的情况</strong> </p>
</li>
<li><p>如果不指定</p>
</li>
</ol>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">next(&#123;<span class="attr">path</span>: <span class="string">&#x27;/login&#x27;</span>&#125;) </span><br></pre></td></tr></table></div></figure>

<p>即<strong>指定去登录页面（前提是登录页面的path是’/login’）</strong>，</p>
<p>那么next()就代表<strong>允许去想要去的页面</strong></p>
<ol start="4">
<li>因为每次我们要看<strong>sessionStorage.getItem(“token”)<strong>里面值是不是’true’。因此，我们需要在登录成功的时候！添加对应的</strong>token</strong>信息</li>
</ol>

        <h2 id="登录成功添加可“允许通过”的路由信息（Token，Session，Cookies均可）"   >
          <a href="#登录成功添加可“允许通过”的路由信息（Token，Session，Cookies均可）" class="heading-link"><i class="fas fa-link"></i></a><a href="#登录成功添加可“允许通过”的路由信息（Token，Session，Cookies均可）" class="headerlink" title="登录成功添加可“允许通过”的路由信息（Token，Session，Cookies均可）"></a>登录成功添加可“允许通过”的路由信息（Token，Session，Cookies均可）</h2>
      <figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sessionStorage.setItem(<span class="string">&quot;token&quot;</span>, <span class="literal">true</span>);</span><br></pre></td></tr></table></div></figure>


        <h2 id="过滤路由显示菜单"   >
          <a href="#过滤路由显示菜单" class="heading-link"><i class="fas fa-link"></i></a><a href="#过滤路由显示菜单" class="headerlink" title="过滤路由显示菜单"></a>过滤路由显示菜单</h2>
      <p><strong>如果在菜单中不想让某些通用模块出现（比如登录、注册、注销等功能）</strong></p>
<p><strong>可以在对应路由信息中，添加属性: <code>hidden: true</code></strong></p>
<ul>
<li>在页面中的el-submenu 以及el-submenu-item中 去for循环遍历时，加一个v-if=”!item.hidden” </li>
</ul>
<p><mark> 注意，v-if 与 v-for 是不能同时出现在属性中 可以将v-for 提前一个标签！</mark></p>

        <h1 id="Vue-项目中加入常用插件"   >
          <a href="#Vue-项目中加入常用插件" class="heading-link"><i class="fas fa-link"></i></a><a href="#Vue-项目中加入常用插件" class="headerlink" title="Vue 项目中加入常用插件"></a>Vue 项目中加入常用插件</h1>
      
        <h2 id="加入插件"   >
          <a href="#加入插件" class="heading-link"><i class="fas fa-link"></i></a><a href="#加入插件" class="headerlink" title="加入插件"></a>加入插件</h2>
      <p><strong>通常，我们通过vue/cli来创建相应的vue项目</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vue create vue_demo</span><br></pre></td></tr></table></div></figure>

<ul>
<li>注意，如果不想要VUE严格的eslint语法规则。</li>
</ul>
<p><strong>可以在创建的时候，取消勾选Fomatter。</strong></p>
<p><strong>若需要VUEX全局状态管理，可以勾选。</strong></p>
<p><strong>若需要路由router，则可以选择勾选。</strong></p>
<p>…在经过漫长的cmd后</p>
<p>不用关闭当前<strong>git bash</strong>或者<strong>命令行控制工具Command</strong></p>
<p>我们首先进入这个项目，直接利用npm配置一些插件。</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">cd vue_demo</span><br><span class="line">vue add vuetify <span class="comment">//下载vuetify移动自适应插件</span></span><br><span class="line">vue add element <span class="comment">//引入elementUI 并且在接下来选择时Fully import还是按需引入</span></span><br><span class="line">vue add axios <span class="comment">//将ajax封装好的axios</span></span><br></pre></td></tr></table></div></figure>

<p><strong>不出意外的话，在具体的项目目录中，可以看到多出一个文件夹plugins。</strong></p>
<p><strong>这里装着我们安装进去的插件。</strong></p>
<p>在配置之后，我们可以用VS code来打开这个文件夹，并且</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm run serve</span><br></pre></td></tr></table></div></figure>

<p>运行项目</p>

        <h1 id="VUE中添加Echarts可视化图表"   >
          <a href="#VUE中添加Echarts可视化图表" class="heading-link"><i class="fas fa-link"></i></a><a href="#VUE中添加Echarts可视化图表" class="headerlink" title="VUE中添加Echarts可视化图表"></a>VUE中添加Echarts可视化图表</h1>
      
        <h2 id="npm-安装echarts插件"   >
          <a href="#npm-安装echarts插件" class="heading-link"><i class="fas fa-link"></i></a><a href="#npm-安装echarts插件" class="headerlink" title="npm 安装echarts插件"></a>npm 安装echarts插件</h2>
      <figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install echarts --save</span><br></pre></td></tr></table></div></figure>

<p><strong>如果npm的安装速度较慢，可以使用cnpm淘宝镜像</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">//安装淘宝镜像</span></span><br><span class="line">npm install -g cnpm --registry=https:<span class="comment">//registry.npm.taobao.org</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//从淘宝镜像下载</span></span><br><span class="line">cnpm install echarts -S</span><br></pre></td></tr></table></div></figure>

<p><strong>当完成npm安装后，echarts插件出现在 node_modules 目录下</strong></p>

        <h2 id="全局引入ECHARTS"   >
          <a href="#全局引入ECHARTS" class="heading-link"><i class="fas fa-link"></i></a><a href="#全局引入ECHARTS" class="headerlink" title="全局引入ECHARTS"></a>全局引入ECHARTS</h2>
      <p><strong>main.js 文件</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 引入echarts</span></span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> echarts <span class="keyword">from</span> <span class="string">&#x27;echarts&#x27;</span></span><br><span class="line"></span><br><span class="line">Vue.prototype.$echarts = echarts</span><br></pre></td></tr></table></div></figure>


        <h2 id="初始化图表"   >
          <a href="#初始化图表" class="heading-link"><i class="fas fa-link"></i></a><a href="#初始化图表" class="headerlink" title="初始化图表"></a>初始化图表</h2>
      <figure class="highlight html"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span>  <span class="attr">ref</span>=<span class="string">&quot;chartColumn&quot;</span> <span class="attr">style</span>=<span class="string">&quot;&#123;width: &#x27;300px&#x27;, height: &#x27;300px&#x27;&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></div></figure>

<ol>
<li><p>ref属性是为了<strong>JS代码中能引用这个图表</strong></p>
</li>
<li><p><code>&lt;div&gt;</code>属性之间默认隔行 如果想要多个图表在同一行中显示 添加属性：<code>style=&quot;display: inline-block&quot;</code></p>
</li>
<li><p>如果想要动态改变 图表的高度与宽度 <strong>可以利用VUE的动态绑定</strong></p>
</li>
</ol>

        <h2 id="JS画图动态渲染（柱状图与饼状图）"   >
          <a href="#JS画图动态渲染（柱状图与饼状图）" class="heading-link"><i class="fas fa-link"></i></a><a href="#JS画图动态渲染（柱状图与饼状图）" class="headerlink" title="JS画图动态渲染（柱状图与饼状图）"></a>JS画图动态渲染（柱状图与饼状图）</h2>
      
        <h3 id="饼状图"   >
          <a href="#饼状图" class="heading-link"><i class="fas fa-link"></i></a><a href="#饼状图" class="headerlink" title="饼状图"></a>饼状图</h3>
      <p><strong>将画图抽象成一个函数 <code>drawLine()</code> 这个函数放在mounted() 生命周期函数中</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">this</span>.drawLine();</span><br></pre></td></tr></table></div></figure>

<p><strong>图标参数JSON数据 （定义在data中的return{}）</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">optionCircle:&#123;</span><br><span class="line">                    <span class="attr">title</span>: &#123;</span><br><span class="line">                        <span class="attr">text</span>: <span class="string">&#x27;实时热门图书&#x27;</span>,</span><br><span class="line">                        <span class="attr">subtext</span>: <span class="string">&#x27;站点动态统计&#x27;</span>,</span><br><span class="line">                        <span class="attr">left</span>: <span class="string">&#x27;center&#x27;</span></span><br><span class="line">                    &#125;,</span><br><span class="line">                    <span class="attr">tooltip</span>: &#123;</span><br><span class="line">                        <span class="attr">trigger</span>: <span class="string">&#x27;item&#x27;</span></span><br><span class="line">                    &#125;,</span><br><span class="line">                    <span class="attr">legend</span>: &#123;</span><br><span class="line">                        <span class="attr">orient</span>: <span class="string">&#x27;vertical&#x27;</span>,</span><br><span class="line">                        <span class="attr">left</span>: <span class="string">&#x27;left&#x27;</span>,</span><br><span class="line">                    &#125;,</span><br><span class="line">                    <span class="attr">series</span>: [</span><br><span class="line">                        &#123;</span><br><span class="line">                            <span class="attr">name</span>: <span class="string">&#x27;访问来源&#x27;</span>,</span><br><span class="line">                            <span class="attr">type</span>: <span class="string">&#x27;pie&#x27;</span>,</span><br><span class="line">                            <span class="attr">radius</span>: <span class="string">&#x27;50%&#x27;</span>,</span><br><span class="line">                            <span class="attr">data</span>: [],</span><br><span class="line">                            <span class="attr">emphasis</span>: &#123;</span><br><span class="line">                                <span class="attr">itemStyle</span>: &#123;</span><br><span class="line">                                    <span class="attr">shadowBlur</span>: <span class="number">10</span>,</span><br><span class="line">                                    <span class="attr">shadowOffsetX</span>: <span class="number">0</span>,</span><br><span class="line">                                    <span class="attr">shadowColor</span>: <span class="string">&#x27;rgba(0, 0, 0, 0.5)&#x27;</span></span><br><span class="line">                                &#125;</span><br><span class="line">                            &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                    ]</span><br><span class="line">                &#125;</span><br></pre></td></tr></table></div></figure>

<p><strong>正常情况下，是看不到任何数据的 但是能看到你的饼状图标题</strong></p>
<p><strong>接下来，我们要在data中从后台动态添加数据</strong></p>
<p><strong>这里我选择 AXIOS 异步刷新数据</strong></p>
<p><strong>JS drawLine() 函数</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">drawLine</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	 <span class="comment">// 基于准备好的dom，初始化echarts实例</span></span><br><span class="line">	 <span class="comment">//通过ref来找到图表</span></span><br><span class="line">	<span class="keyword">let</span> myChartCircle = <span class="built_in">this</span>.$echarts.init(<span class="built_in">this</span>.$refs.chartCircle);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//动态从数据库中取数据</span></span><br><span class="line">	<span class="comment">//画饼图</span></span><br><span class="line">	<span class="keyword">const</span> _this = <span class="built_in">this</span></span><br><span class="line">	axios.get(</span><br><span class="line">		main.requestAddress +</span><br><span class="line">		<span class="string">&#x27;/book/showPopularBooks&#x27;</span>).then(<span class="function"><span class="keyword">function</span> (<span class="params">response</span>) </span>&#123;</span><br><span class="line">		<span class="keyword">let</span> list = response.data.data</span><br><span class="line">		<span class="keyword">let</span> data = _this.$data.optionCircle.series[<span class="number">0</span>].data</span><br><span class="line"></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">let</span> i =<span class="number">0</span>; i&lt;list.length; i++)&#123;</span><br><span class="line">			data.push(&#123;</span><br><span class="line">				<span class="attr">name</span>: list[i].bookname,</span><br><span class="line">				<span class="attr">value</span>: list[i].borrowNum</span><br><span class="line">			&#125;)</span><br><span class="line">		&#125;</span><br><span class="line">		myChartCircle.setOption(_this.$data.optionCircle);</span><br><span class="line">	&#125;).catch(<span class="function"><span class="keyword">function</span> (<span class="params">error</span>) </span>&#123;</span><br><span class="line">		alert(error);</span><br><span class="line">	&#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p><strong>可以看到，其实真正的饼状图数据是：</strong></p>
<p><strong>“name”代表饼图的名称 “value”代表对应name的饼图数据</strong></p>
<p><strong>值得注意的是，这种push方法是加入新的JSON对象的方法</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">data.push(&#123;</span><br><span class="line">	<span class="attr">key1</span>: value1(可以来自后台),</span><br><span class="line">	<span class="attr">key2</span>: value2(可以来自后台),</span><br><span class="line">	...</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></div></figure>


<p><strong>效果图如下：</strong></p>
<p><img   src="https://img-blog.csdnimg.cn/20210416123833199.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>

        <h3 id="柱状图"   >
          <a href="#柱状图" class="heading-link"><i class="fas fa-link"></i></a><a href="#柱状图" class="headerlink" title="柱状图"></a>柱状图</h3>
      <p><strong>与饼状图相似，先是配置图标参数，渲染图表，最后是取数据</strong></p>

        <h4 id="配置图表参数"   >
          <a href="#配置图表参数" class="heading-link"><i class="fas fa-link"></i></a><a href="#配置图表参数" class="headerlink" title="配置图表参数"></a>配置图表参数</h4>
      <figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">optionColumn:&#123;</span><br><span class="line">		<span class="attr">title</span>: &#123;</span><br><span class="line">			<span class="attr">text</span>: <span class="string">&#x27;实时借书用户&#x27;</span></span><br><span class="line">		&#125;,</span><br><span class="line">		<span class="attr">tooltip</span>: &#123;</span><br><span class="line">		&#125;,</span><br><span class="line">		<span class="attr">legend</span>: &#123;</span><br><span class="line">		&#125;,</span><br><span class="line">		<span class="attr">xAxis</span>: &#123;</span><br><span class="line">			<span class="attr">data</span>: []</span><br><span class="line">		&#125;,</span><br><span class="line">		<span class="attr">yAxis</span>: &#123;&#125;,</span><br><span class="line">		<span class="attr">series</span>: [&#123;</span><br><span class="line">			<span class="attr">name</span>: <span class="string">&#x27;借阅次数&#x27;</span>,</span><br><span class="line">			<span class="attr">type</span>: <span class="string">&#x27;bar&#x27;</span>,</span><br><span class="line">			<span class="attr">data</span>: []</span><br><span class="line">		&#125;]</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></div></figure>


        <h4 id="渲染图表"   >
          <a href="#渲染图表" class="heading-link"><i class="fas fa-link"></i></a><a href="#渲染图表" class="headerlink" title="渲染图表"></a>渲染图表</h4>
      <figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> myChartColumn = <span class="built_in">this</span>.$echarts.init(<span class="built_in">this</span>.$refs.chartColumn)</span><br><span class="line">	<span class="comment">//使用刚指定的配置项和数据显示图表。</span></span><br><span class="line">	<span class="comment">//画柱状图</span></span><br><span class="line">	axios.get(</span><br><span class="line">		main.requestAddress +</span><br><span class="line">		<span class="string">&#x27;/book/showPopularUsers&#x27;</span>).then(<span class="function"><span class="keyword">function</span> (<span class="params">response</span>) </span>&#123;</span><br><span class="line">		<span class="keyword">let</span> list = response.data.data</span><br><span class="line">		<span class="keyword">let</span> nameArray = _this.$data.optionColumn.xAxis.data</span><br><span class="line">		<span class="keyword">let</span> numArray = _this.$data.optionColumn.series[<span class="number">0</span>].data</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">let</span> i=<span class="number">0</span>; i&lt;list.length; i++)&#123;</span><br><span class="line">			nameArray.push(list[i].username);</span><br><span class="line">			numArray.push(list[i].totalBorrowNum);</span><br><span class="line">		&#125;</span><br><span class="line">		myChartColumn.setOption(_this.$data.optionColumn);</span><br><span class="line"></span><br><span class="line">	&#125;).catch(<span class="function"><span class="keyword">function</span> (<span class="params">error</span>) </span>&#123;</span><br><span class="line">		alert(error);</span><br><span class="line">	&#125;);</span><br></pre></td></tr></table></div></figure>

<p><strong>可以看到，对于柱状图，我们要配置两个</strong></p>
<ol>
<li><p>xAxis 即X轴的数组中的数据</p>
</li>
<li><p>series[0]中的data数组 代表从左往右依次的 棱柱所代表的value</p>
</li>
</ol>
<p><strong>效果图如下</strong></p>
<p><img   src="https://img-blog.csdnimg.cn/2021041612451596.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>

        <h1 id="VUEX-存值与取值区域"   >
          <a href="#VUEX-存值与取值区域" class="heading-link"><i class="fas fa-link"></i></a><a href="#VUEX-存值与取值区域" class="headerlink" title="VUEX 存值与取值区域"></a>VUEX 存值与取值区域</h1>
      <p><strong>在VUE中，如果有些数据我们不太需要后台频繁的查询数据</strong></p>
<p>我们就可以将这些数据<strong>存放在VUEX中</strong></p>

        <h2 id="声明store的index-js"   >
          <a href="#声明store的index-js" class="heading-link"><i class="fas fa-link"></i></a><a href="#声明store的index-js" class="headerlink" title="声明store的index.js"></a>声明store的index.js</h2>
      <figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Vuex <span class="keyword">from</span> <span class="string">&#x27;vuex&#x27;</span></span><br><span class="line"></span><br><span class="line">Vue.use(Vuex)</span><br><span class="line"></span><br><span class="line"><span class="comment">//导出之后 外部可以使用</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">new</span> Vuex.Store(&#123;</span><br><span class="line">	<span class="attr">state</span>:&#123;</span><br><span class="line">		<span class="comment">//放全局变量 比如viewCounter是浏览量统计器</span></span><br><span class="line">		viewCounter = <span class="number">1</span></span><br><span class="line">	&#125;,</span><br><span class="line">	<span class="attr">mutations</span>:&#123;</span><br><span class="line">		<span class="comment">//setter方法区域</span></span><br><span class="line">		<span class="function"><span class="title">setViewCounter</span>(<span class="params">state, viewCounter</span>)</span>&#123;</span><br><span class="line">			state.viewCounter = viewCounter </span><br><span class="line">		&#125;</span><br><span class="line">	&#125;,</span><br><span class="line">	<span class="attr">getters</span>:&#123;</span><br><span class="line">		<span class="comment">//getter方法区域</span></span><br><span class="line">		<span class="attr">getViewCounter</span>: <span class="function"><span class="params">state</span> =&gt;</span> state.viewCounter</span><br><span class="line">	&#125;,</span><br><span class="line">	<span class="attr">action</span>:&#123;&#125;,</span><br><span class="line">	<span class="attr">modules</span>:&#123;&#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>



        <h2 id="在需要存值、取值的地方-调用方法"   >
          <a href="#在需要存值、取值的地方-调用方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#在需要存值、取值的地方-调用方法" class="headerlink" title="在需要存值、取值的地方 调用方法"></a>在需要存值、取值的地方 调用方法</h2>
      
        <h3 id="取值getViewCounter"   >
          <a href="#取值getViewCounter" class="heading-link"><i class="fas fa-link"></i></a><a href="#取值getViewCounter" class="headerlink" title="取值getViewCounter"></a>取值getViewCounter</h3>
      <figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">this</span>.$store.getters.getViewCounter</span><br></pre></td></tr></table></div></figure>


        <h3 id="存值setViewCounter方法"   >
          <a href="#存值setViewCounter方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#存值setViewCounter方法" class="headerlink" title="存值setViewCounter方法"></a>存值setViewCounter方法</h3>
      <figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">this</span>.$store.commit(<span class="string">&#x27;setViewCounter&#x27;</span>, newValue);</span><br></pre></td></tr></table></div></figure>


        <h1 id="WEB项目实现文件下载与上传"   >
          <a href="#WEB项目实现文件下载与上传" class="heading-link"><i class="fas fa-link"></i></a><a href="#WEB项目实现文件下载与上传" class="headerlink" title="WEB项目实现文件下载与上传"></a>WEB项目实现文件下载与上传</h1>
      
        <h2 id="SSM项目-非MultipartFile类"   >
          <a href="#SSM项目-非MultipartFile类" class="heading-link"><i class="fas fa-link"></i></a><a href="#SSM项目-非MultipartFile类" class="headerlink" title="SSM项目+非MultipartFile类"></a>SSM项目+非MultipartFile类</h2>
      <ul>
<li>在这种类型的项目中，我们需要在相应的Spring MVC.xml配置文件中，去配置一个Bean</li>
</ul>
<p>代码如下：</p>
<figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;multipartResolver&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.web.multipart.commons.CommonsMultipartResolver&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;maxUploadSize&quot;</span> <span class="attr">value</span>=<span class="string">&quot;104857600&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;maxInMemorySize&quot;</span> <span class="attr">value</span>=<span class="string">&quot;4096&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;defaultEncoding&quot;</span> <span class="attr">value</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></div></figure>

<p>这种，我们就可以在Controller层中，使用@RequestParam(“file”) MultipartFile file了。</p>
<p>如果前端是Form表单的话，则前端代码如下：</p>
<figure class="highlight html"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">form</span> <span class="attr">class</span>=<span class="string">&quot;form-horizontal &quot;</span> <span class="attr">action</span>=<span class="string">&quot;/UploadServlet&quot;</span> <span class="attr">id</span>=<span class="string">&quot;upload&quot;</span> <span class="attr">enctype</span>=<span class="string">&quot;multipart/form-data&quot;</span> <span class="attr">method</span>=<span class="string">&quot;post&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">input</span> <span class="attr">class</span>=<span class="string">&quot;form-control&quot;</span> <span class="attr">type</span>=<span class="string">&quot;file&quot;</span>&gt;</span></span><br><span class="line">	 <span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;submit&quot;</span> <span class="attr">class</span>=<span class="string">&quot;btn btn-success btn-sm&quot;</span>&gt;</span>上传<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">	 <span class="tag">&lt;<span class="name">div</span>&gt;</span> $&#123; message &#125; <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br></pre></td></tr></table></div></figure>

<p><strong>这样，当我们点击上传后，即会跳转到对应的action，我们用@PostMapping来接收即可。</strong></p>
<p>后台代码如下：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">	<span class="comment">// 上传文件存储目录</span></span><br><span class="line">   <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String UPLOAD_DIRECTORY = <span class="string">&quot;upload&quot;</span>;</span><br><span class="line"></span><br><span class="line">   <span class="comment">// 上传配置</span></span><br><span class="line">   <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MEMORY_THRESHOLD = <span class="number">1024</span> * <span class="number">1024</span> * <span class="number">3</span>;  <span class="comment">// 3MB</span></span><br><span class="line">   <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MAX_FILE_SIZE = <span class="number">1024</span> * <span class="number">1024</span> * <span class="number">40</span>; <span class="comment">// 40MB</span></span><br><span class="line">   <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MAX_REQUEST_SIZE = <span class="number">1024</span> * <span class="number">1024</span> * <span class="number">50</span>; <span class="comment">// 50MB</span></span><br><span class="line">   </span><br><span class="line"><span class="meta">@RequestMapping(value = &quot;/UploadServlet&quot;, method = RequestMethod.POST)</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> String <span class="title">upload</span><span class="params">(</span></span></span><br><span class="line"><span class="params"><span class="function">           ModelAndView modelAndView,</span></span></span><br><span class="line"><span class="params"><span class="function">           HttpServletRequest request,</span></span></span><br><span class="line"><span class="params"><span class="function">           HttpServletResponse response)</span> <span class="keyword">throws</span> IOException, ServletException </span>&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">       <span class="comment">// 配置上传参数</span></span><br><span class="line">       DiskFileItemFactory factory = <span class="keyword">new</span> DiskFileItemFactory();</span><br><span class="line">       <span class="comment">// 设置内存临界值 - 超过后将产生临时文件并存储于临时目录中</span></span><br><span class="line">       factory.setSizeThreshold(MEMORY_THRESHOLD);</span><br><span class="line">       <span class="comment">// 设置临时存储目录</span></span><br><span class="line">       factory.setRepository(<span class="keyword">new</span> File(System.getProperty(<span class="string">&quot;java.io.tmpdir&quot;</span>)));</span><br><span class="line"></span><br><span class="line">       ServletFileUpload upload = <span class="keyword">new</span> ServletFileUpload(factory);</span><br><span class="line"></span><br><span class="line">       <span class="comment">// 设置最大文件上传值</span></span><br><span class="line">       upload.setFileSizeMax(MAX_FILE_SIZE);</span><br><span class="line"></span><br><span class="line">       <span class="comment">// 设置最大请求值 (包含文件和表单数据)</span></span><br><span class="line">       upload.setSizeMax(MAX_REQUEST_SIZE);</span><br><span class="line"></span><br><span class="line">       <span class="comment">// 中文处理</span></span><br><span class="line">       upload.setHeaderEncoding(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line"></span><br><span class="line">       <span class="comment">// 构造临时路径来存储上传的文件</span></span><br><span class="line">       <span class="comment">// 这个路径相对当前应用的目录</span></span><br><span class="line">       String uploadPath = request.getServletContext().getRealPath(<span class="string">&quot;./&quot;</span>) + File.separator + UPLOAD_DIRECTORY;</span><br><span class="line"></span><br><span class="line">       <span class="comment">// 如果目录不存在则创建</span></span><br><span class="line">       File uploadDir = <span class="keyword">new</span> File(uploadPath);</span><br><span class="line">       <span class="keyword">if</span> (!uploadDir.exists()) &#123;</span><br><span class="line">           uploadDir.mkdir();</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line">       <span class="keyword">try</span> &#123;</span><br><span class="line">           List&lt;FileItem&gt; formItems = upload.parseRequest(request);</span><br><span class="line">           <span class="keyword">if</span> (formItems != <span class="keyword">null</span> &amp;&amp; formItems.size() &gt; <span class="number">0</span>) &#123;</span><br><span class="line">               <span class="comment">// 迭代表单数据</span></span><br><span class="line">               <span class="keyword">for</span> (FileItem item : formItems) &#123;</span><br><span class="line">                   <span class="comment">// 处理不在表单中的字段</span></span><br><span class="line">                   <span class="keyword">if</span> (!item.isFormField()) &#123;</span><br><span class="line">                       String fileName = <span class="keyword">new</span> File(item.getName()).getName();</span><br><span class="line">                       String filePath = uploadPath + File.separator + fileName;</span><br><span class="line">                       File storeFile = <span class="keyword">new</span> File(filePath);</span><br><span class="line"></span><br><span class="line">                       <span class="comment">// 在控制台输出文件的上传路径</span></span><br><span class="line">                       String filePathSaved = uploadPath + File.separator;</span><br><span class="line"></span><br><span class="line">                       <span class="comment">// 保存文件到硬盘</span></span><br><span class="line">                       item.write(storeFile);</span><br><span class="line">                       teacherServer.saveFilePath(filePathSaved, fileName);</span><br><span class="line">                       request.setAttribute(<span class="string">&quot;message&quot;</span>, formItems.get(<span class="number">0</span>).getName() + <span class="string">&quot;文件上传成功!&quot;</span>);</span><br><span class="line">                   &#125;</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125; <span class="keyword">catch</span> (Exception ex) &#123;</span><br><span class="line">           request.setAttribute(<span class="string">&quot;message&quot;</span>, <span class="string">&quot;错误信息: &quot;</span> + <span class="string">&quot;文件上传失败，请检查是否文件为空！&quot;</span>);</span><br><span class="line"></span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">return</span> <span class="string">&quot;/teacher/tea_add_paper&quot;</span>;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></div></figure>

<p><strong>注意到return这里，我们需要配置相应的视图解析器，不然我们不知道return哪个地方去显示。</strong></p>
<figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--配置试图解析--&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span> = <span class="string">&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;prefix&quot;</span> <span class="attr">value</span>=<span class="string">&quot;/&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;suffix&quot;</span> <span class="attr">value</span>=<span class="string">&quot;.jsp&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></div></figure>

<p><img   src="https://img-blog.csdnimg.cn/20210514102019792.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"><br><strong>注意：webapp 就是我们配置的根目录，并且配置的视图后缀为.jsp</strong></p>
<ul>
<li>这样我们就能在，相应的视图中，看到message的内容了</li>
</ul>

        <h2 id="SpringBoot项目-JSP-MultipartFile（非AJAX异步）"   >
          <a href="#SpringBoot项目-JSP-MultipartFile（非AJAX异步）" class="heading-link"><i class="fas fa-link"></i></a><a href="#SpringBoot项目-JSP-MultipartFile（非AJAX异步）" class="headerlink" title="SpringBoot项目+JSP+MultipartFile（非AJAX异步）"></a>SpringBoot项目+JSP+MultipartFile（非AJAX异步）</h2>
      <p><strong>首先，我们需要在SpringBoot的配置文件application.yml或者application.properties中配置：</strong></p>
<p><strong>application.properties</strong></p>
<figure class="highlight yml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">spring.servlet.multipart.max-request-size=10MB</span></span><br><span class="line"></span><br><span class="line"><span class="string">spring.servlet.multipart.max-file-size=10MB</span></span><br></pre></td></tr></table></div></figure>

<p><strong>然后我们，在controller中写相应语句：</strong></p>
<ul>
<li>我们仍然需要配置视图解析器ViewResolver：</li>
</ul>
<p><strong>在application.properties中：</strong></p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">spring.mvc.view.prefix=/</span><br><span class="line"></span><br><span class="line">spring.mvc.view.suffix=.jsp</span><br></pre></td></tr></table></div></figure>
<p>仍与上方图片中的项目结构图一致！</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">   <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String UPLOAD_DIRECTORY = <span class="string">&quot;upload&quot;</span>;</span><br><span class="line">   </span><br><span class="line"><span class="meta">@PostMapping(&quot;/upload&quot;)</span></span><br><span class="line">   <span class="meta">@ResponseBody</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> String <span class="title">upload</span><span class="params">(<span class="meta">@RequestParam(&quot;file&quot;)</span> MultipartFile file, HttpServletRequest request)</span> </span>&#123;</span><br><span class="line">       <span class="keyword">if</span> (file.isEmpty()) &#123;</span><br><span class="line">       	request.setAttribute(<span class="string">&quot;message&quot;</span>, <span class="string">&quot;对不起，检测到文件为空！&quot;</span>);</span><br><span class="line">           <span class="keyword">return</span> <span class="string">&quot;/teacher/tea_add_paper&quot;</span>;</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line">       String fileName = file.getOriginalFilename();</span><br><span class="line">       String templatePath = request.getServletContext().getRealPath(<span class="string">&quot;./&quot;</span>) + File.separator + UPLOAD_DIRECTORY;</span><br><span class="line"></span><br><span class="line">       File dest = <span class="keyword">new</span> File(filePath + fileName);</span><br><span class="line">       <span class="keyword">try</span> &#123;</span><br><span class="line">           file.transferTo(dest);</span><br><span class="line">           request.setAttribute(<span class="string">&quot;message&quot;</span>, <span class="string">&quot;文件上传成功！&quot;</span>);</span><br><span class="line">           <span class="keyword">return</span> <span class="string">&quot;/teacher/tea_add_paper&quot;</span>;</span><br><span class="line">       &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">       	System.out.println(e)</span><br><span class="line">       &#125;</span><br><span class="line">       </span><br><span class="line">       request.setAttribute(<span class="string">&quot;message&quot;</span>, <span class="string">&quot;文件上传失败！&quot;</span>);</span><br><span class="line">       <span class="keyword">return</span> <span class="string">&quot;/teacher/tea_add_paper&quot;</span>;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></div></figure>

<p><strong>接下来，就准备写前端页面请求：</strong></p>
<figure class="highlight html"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">form</span> <span class="attr">method</span>=<span class="string">&quot;post&quot;</span> <span class="attr">action</span>=<span class="string">&quot;/uploading&quot;</span> <span class="attr">enctype</span>=<span class="string">&quot;multipart/form-data&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;file&quot;</span> <span class="attr">name</span>=<span class="string">&quot;file&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;submit&quot;</span> <span class="attr">value</span>=<span class="string">&quot;提交给服务器&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    	$&#123;message&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="SpringBoot-VUE-ElementUI前后端分离"   >
          <a href="#SpringBoot-VUE-ElementUI前后端分离" class="heading-link"><i class="fas fa-link"></i></a><a href="#SpringBoot-VUE-ElementUI前后端分离" class="headerlink" title="SpringBoot+VUE+ElementUI前后端分离"></a>SpringBoot+VUE+ElementUI前后端分离</h2>
      <p><strong>前提是解决跨域问题，并且直接通过IP地址访问，将这个访问的ip地址存在VUEX中或者Main.js中即可。</strong></p>
<p><strong>1. VUE前端页面</strong></p>
<figure class="highlight html"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">el-upload</span></span></span><br><span class="line"><span class="tag">            <span class="attr">class</span>=<span class="string">&quot;uploadDemo&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">ref</span>=<span class="string">&quot;upload&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">accept</span>=<span class="string">&quot;.doc&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">action</span>=<span class="string">&quot;https://jsonplaceholder.typicode.com/posts/&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">:on-preview</span>=<span class="string">&quot;handlePreview&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">:on-remove</span>=<span class="string">&quot;handleRemove&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">:http-request</span>=<span class="string">&quot;httpRequest&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">:auto-upload</span>=<span class="string">&quot;false&quot;</span></span></span><br><span class="line"><span class="tag">          &gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">el-button</span> <span class="attr">slot</span>=<span class="string">&quot;trigger&quot;</span> <span class="attr">size</span>=<span class="string">&quot;small&quot;</span> <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span></span></span><br><span class="line"><span class="tag">              &gt;</span>选取文件&lt;/el-button</span><br><span class="line">            &gt;</span><br><span class="line">            <span class="tag">&lt;<span class="name">el-button</span></span></span><br><span class="line"><span class="tag">              <span class="attr">style</span>=<span class="string">&quot;margin-left: 10px&quot;</span></span></span><br><span class="line"><span class="tag">              <span class="attr">size</span>=<span class="string">&quot;small&quot;</span></span></span><br><span class="line"><span class="tag">              <span class="attr">type</span>=<span class="string">&quot;success&quot;</span></span></span><br><span class="line"><span class="tag">              @<span class="attr">click</span>=<span class="string">&quot;submitUpload&quot;</span></span></span><br><span class="line"><span class="tag">              &gt;</span>上传到服务器&lt;/el-button</span><br><span class="line">            &gt;</span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span> <span class="attr">slot</span>=<span class="string">&quot;tip&quot;</span> <span class="attr">class</span>=<span class="string">&quot;el-upload__tip&quot;</span>&gt;</span></span><br><span class="line">              只能上传.doc后缀文件，且不超过40MB</span><br><span class="line">            <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;/<span class="name">el-upload</span>&gt;</span></span><br></pre></td></tr></table></div></figure>

<ol start="2">
<li>写两个函数</li>
</ol>
<p><strong>一个是按钮点击函数，另一个是上传文件的表单具体提交（axios请求）的函数</strong></p>
<ul>
<li>提交按钮函数<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">submitUpload</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">      <span class="built_in">this</span>.$refs.upload.submit(); <span class="comment">//我们通过el-upload的引用来找到 并让他提交</span></span><br><span class="line">    &#125;,</span><br></pre></td></tr></table></div></figure></li>
</ul>
<p><strong>注意，我们这里看到他的action位置，并不是我们的后台地址。</strong></p>
<p><strong>也就是说，我们通过先把自己的文件传给他的action，再由他的action服务器地址，传给我们的后台服务器。</strong></p>
<p><strong>最后，提交表单后，会走绑定属性httpRequest的方法，即：</strong></p>
<ul>
<li>表单提交函数<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//最后上传走这个方法！</span></span><br><span class="line">    <span class="function"><span class="title">httpRequest</span>(<span class="params">param</span>)</span> &#123;</span><br><span class="line">      <span class="keyword">let</span> fileObj = param.file; <span class="comment">// 相当于input里取得的files</span></span><br><span class="line">      <span class="keyword">let</span> data = <span class="keyword">new</span> FormData(); <span class="comment">// FormData 对象</span></span><br><span class="line">      data.append(<span class="string">&quot;file&quot;</span>, fileObj); <span class="comment">// 文件对象 后台用@RequestParam来接收</span></span><br><span class="line">      <span class="keyword">const</span> config = &#123; <span class="attr">headers</span>: &#123; <span class="string">&quot;Content-Type&quot;</span>: <span class="string">&quot;multipart/form-data&quot;</span> &#125; &#125;;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">var</span> _this = <span class="built_in">this</span>;</span><br><span class="line">	  <span class="comment">//在这里我将后台的服务器地址 存进了VUEX中</span></span><br><span class="line">	  <span class="comment">//并通过相应的getters 来获取</span></span><br><span class="line">      <span class="built_in">this</span>.$axios</span><br><span class="line">        .post(</span><br><span class="line">          <span class="built_in">this</span>.$store.getters.getRequestAddress + <span class="string">`/uploading`</span>,</span><br><span class="line">          data,</span><br><span class="line">          config</span><br><span class="line">        )</span><br><span class="line">        .then(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">          <span class="keyword">let</span> data = res.data;</span><br><span class="line">          <span class="built_in">console</span>.log(data);</span><br><span class="line">          <span class="keyword">if</span> (data.data === <span class="number">1</span>) &#123;</span><br><span class="line">            _this.sendMessage(data.message, <span class="string">&quot;success&quot;</span>);</span><br><span class="line">          &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            _this.sendMessage(data.message, <span class="string">&quot;error&quot;</span>);</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">        .catch(<span class="function">(<span class="params"><span class="built_in">Error</span></span>) =&gt;</span> &#123;</span><br><span class="line">          _this.sendMessage(<span class="string">&quot;请求失败！&quot;</span>, <span class="string">&quot;error&quot;</span>);</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;,</span><br></pre></td></tr></table></div></figure></li>
</ul>
<p><strong>发送消息的方法，记得引入ElementUI</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//弹窗发送消息 以传入的参数而定</span></span><br><span class="line">    <span class="function"><span class="title">sendMessage</span>(<span class="params">messageContent, messageType</span>)</span> &#123;</span><br><span class="line">      <span class="built_in">this</span>.$message(&#123;</span><br><span class="line">        <span class="attr">message</span>: messageContent,</span><br><span class="line">        <span class="attr">type</span>: messageType,</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;,</span><br></pre></td></tr></table></div></figure>

<p><strong>然后就是后台接收（如果是SSM项目，在SpringMVC配置文件中配置Bean</strong></p>
<p><strong>如果是SpringBoot项目，记得在application.properties中配置：</strong></p>
<p><mark> 见上方内容 </mark></p>
<p><strong>接下来，我们写后台的具体上传方法。</strong></p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//elementUI 上传方法</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String UPLOAD_DIRECTORY = <span class="string">&quot;upload&quot;</span>;</span><br><span class="line">	</span><br><span class="line">    <span class="meta">@PostMapping(value = &quot;/uploading&quot;)</span></span><br><span class="line">    <span class="meta">@ResponseBody</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> TSMResult <span class="title">uploadFile</span><span class="params">(<span class="meta">@RequestParam(&quot;file&quot;)</span> MultipartFile file, HttpServletRequest request, TSMResult tsmResult)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (file.isEmpty()) &#123;</span><br><span class="line">            tsmResult.setData(<span class="number">0</span>);</span><br><span class="line">            tsmResult.setMessage(<span class="string">&quot;文件不可为空！&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span> tsmResult;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 获取文件全名a.py</span></span><br><span class="line">        String fileName = file.getOriginalFilename();</span><br><span class="line">        <span class="comment">// 文件上传路径&lt;br&gt;        String templatePath = &quot;E:/file/template/&quot;</span></span><br><span class="line">        String templatePath = request.getServletContext().getRealPath(<span class="string">&quot;./&quot;</span>) + File.separator + UPLOAD_DIRECTORY;</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;文件路径：&quot;</span> + templatePath);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 获取文件的后缀名</span></span><br><span class="line">        String suffixName = fileName.substring(fileName.lastIndexOf(<span class="string">&quot;.&quot;</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">//获取文件名</span></span><br><span class="line">        String prefixName = fileName.substring(<span class="number">0</span>, fileName.lastIndexOf(<span class="string">&quot;.&quot;</span>));</span><br><span class="line">        <span class="comment">// 解决中文问题,liunx 下中文路径,图片显示问题</span></span><br><span class="line">        <span class="comment">//fileName = UUID.randomUUID() + suffixName;</span></span><br><span class="line">        File dest0 = <span class="keyword">new</span> File(templatePath);</span><br><span class="line">        <span class="comment">//prefixName + File.separator +</span></span><br><span class="line">        File dest = <span class="keyword">new</span> File(dest0, fileName);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">//文件上传-覆盖</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">// 检测是否存在目录</span></span><br><span class="line">            <span class="keyword">if</span> (!dest0.getParentFile().exists()) &#123;</span><br><span class="line">                dest0.getParentFile().mkdirs();</span><br><span class="line">                <span class="comment">//检测文件是否存在</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (!dest.exists()) &#123;</span><br><span class="line">                dest.mkdirs();</span><br><span class="line">            &#125;</span><br><span class="line">            file.transferTo(dest);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//上传成功后 将上传路径上传到数据库中</span></span><br><span class="line">            String filePathSaved = templatePath + File.separator;</span><br><span class="line">            teacherServer.saveFilePath(filePathSaved, fileName);</span><br><span class="line"></span><br><span class="line">            tsmResult.setData(<span class="number">1</span>);</span><br><span class="line">            tsmResult.setMessage(<span class="string">&quot;文件上传成功！&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span> tsmResult;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            tsmResult.setData(<span class="number">0</span>);</span><br><span class="line">            tsmResult.setMessage(<span class="string">&quot;服务器错误，请稍后再试！&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span> tsmResult;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></div></figure>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">teacherServer.saveFilePath(filePathSaved, fileName);</span><br></pre></td></tr></table></div></figure>

<p>这个代表，我们<strong>将这个上传的文件名字，以及对应的上传路径，写进数据库中</strong>。</p>
<p>对应服务层代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">saveFilePath</span><span class="params">(String filePathSaved, String fileName)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> sourceMapper.saveFilePath(filePathSaved, fileName);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></div></figure>

<p>对应mapper代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Insert(&quot;insert into source(source, papername) values (#&#123;filePath&#125;,#&#123;fileName&#125;)&quot;)</span></span><br><span class="line">   <span class="function"><span class="keyword">int</span> <span class="title">saveFilePath</span><span class="params">(<span class="meta">@Param(&quot;filePath&quot;)</span> String filePath, <span class="meta">@Param(&quot;fileName&quot;)</span> String fileName)</span></span>;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>

<p>可以建立一个Source实体类，引入Lombok</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> Integer id;</span><br><span class="line"><span class="keyword">private</span> String filePath;</span><br><span class="line"><span class="keyword">private</span> String fileName;</span><br></pre></td></tr></table></div></figure>

<p><strong>并且数据库也建立一个source表，其中字段名与这个Source实体类的属性名一模一样（避免Mybatis的字段映射ResultMap）</strong></p>

        <h1 id="实现文件下载"   >
          <a href="#实现文件下载" class="heading-link"><i class="fas fa-link"></i></a><a href="#实现文件下载" class="headerlink" title="实现文件下载"></a>实现文件下载</h1>
      <p><strong># 在实现文件的上传后，我们肯定会有相应的下载功能。</strong></p>
<p><em>即从上传目录中的文件中，去下载对应的文件。</em></p>
<ul>
<li><p>需求：我们将文件的信息以及路径，存进数据库了，也就代表：</p>
</li>
<li><p>我们“只能下载那些已经保存在数据库中的文件”</p>
</li>
</ul>

        <h2 id="JSP实现文件下载（且动态检测文件是否可下载）"   >
          <a href="#JSP实现文件下载（且动态检测文件是否可下载）" class="heading-link"><i class="fas fa-link"></i></a><a href="#JSP实现文件下载（且动态检测文件是否可下载）" class="headerlink" title="JSP实现文件下载（且动态检测文件是否可下载）"></a>JSP实现文件下载（且动态检测文件是否可下载）</h2>
      <ol>
<li>前端页面动态生成一个锚点（Bootstrap可加上按钮样式类）<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1. 利用Ajax异步获取到分好页的论文（PageHelper插件）</span></span><br><span class="line"><span class="keyword">var</span> list = result.data.list</span><br><span class="line"></span><br><span class="line"><span class="comment">// 2. 利用$.each来遍历这些数据 对每个数据进行筛选</span></span><br><span class="line">$.each(list, <span class="function"><span class="keyword">function</span> (<span class="params">index, item</span>) </span>&#123;	</span><br><span class="line">	<span class="comment">//3. 首先获取到即将下载的论文名(鉴于我们上传时 是将论文的文件名存进数据库的)</span></span><br><span class="line">	<span class="keyword">var</span> papername = item.papername  </span><br><span class="line">	</span><br><span class="line">	<span class="comment">//4. 为每一项“模拟按钮” 动态加上类 方便我们后期动态remove掉</span></span><br><span class="line">	<span class="keyword">var</span> download = $(<span class="string">&quot;&lt;a&gt;&lt;/a&gt;&quot;</span>).addClass(papername + <span class="string">&quot; btn btn-danger btn-sm&quot;</span>).append($(<span class="string">&quot;&lt;span&gt;&lt;/span&gt;&quot;</span>).addClass(<span class="string">&quot;glyphicon glyphicon-download&quot;</span>)).append(<span class="string">&quot;下载&quot;</span>).attr(<span class="string">&quot;href&quot;</span>, <span class="string">&quot;/DownloadServlet?paperName=&quot;</span> + item.papername);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></div></figure></li>
</ol>
<p><strong>注意到，我们这里的路径是：/DownloadServlet?paperName=xxx</strong></p>
<p>因此，我们肯定是以<code>get</code>的请求方式</p>
<ol start="2">
<li>后台controller层代码<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String UPLOAD_DIRECTORY = <span class="string">&quot;upload&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">@RequestMapping(value = &quot;/DownloadServlet&quot;)</span></span><br><span class="line">   <span class="keyword">public</span> ResponseEntity&lt;<span class="keyword">byte</span>[]&gt; fileDownload(</span><br><span class="line">           HttpServletRequest request,</span><br><span class="line">           <span class="meta">@RequestParam(&quot;paperName&quot;)</span> String fileName,</span><br><span class="line">           Model model) <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">       fileName = <span class="keyword">new</span> String(fileName.getBytes(<span class="string">&quot;ISO-8859-1&quot;</span>), <span class="string">&quot;UTF-8&quot;</span>); <span class="comment">//后台接受中文数据编码</span></span><br><span class="line">       fileName = fileName + <span class="string">&quot;.doc&quot;</span>;</span><br><span class="line"></span><br><span class="line">       String filePath = request.getServletContext().getRealPath(<span class="string">&quot;./&quot;</span>) + File.separator + UPLOAD_DIRECTORY + <span class="string">&quot;\\&quot;</span> + fileName;</span><br><span class="line">       File file = <span class="keyword">new</span> File(filePath);</span><br><span class="line">       HttpHeaders headers = <span class="keyword">new</span> HttpHeaders();</span><br><span class="line">       String downloadFile = <span class="keyword">new</span> String(fileName.getBytes(<span class="string">&quot;utf-8&quot;</span>), <span class="string">&quot;iso-8859-1&quot;</span>);</span><br><span class="line">       headers.setContentDispositionFormData(<span class="string">&quot;attachment&quot;</span>, downloadFile);</span><br><span class="line">       headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);</span><br><span class="line">       <span class="keyword">return</span> <span class="keyword">new</span> ResponseEntity&lt;<span class="keyword">byte</span>[]&gt;(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></div></figure></li>
</ol>
<p><strong>其实，上述代码是不够完整的，因为如果数据库中没有这个以.doc为后缀（后缀写死了）的文件，就会报错。</strong></p>
<p><strong>因此，我们需要在前端下载之前就进行一次查询：</strong></p>
<p><strong>如果该文件的文件名存到数据库里了，才给予下载（即才动态添加下载的锚点样式）</strong></p>
<ul>
<li>完整代码：写在$.each遍历中：<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// 1. 利用Ajax异步获取到分好页的论文（PageHelper插件）</span></span><br><span class="line"><span class="keyword">var</span> list = result.data.list</span><br><span class="line"></span><br><span class="line"><span class="comment">// 2. 利用$.each来遍历这些数据 对每个数据进行筛选</span></span><br><span class="line">$.each(list, <span class="function"><span class="keyword">function</span> (<span class="params">index, item</span>) </span>&#123;	</span><br><span class="line">	<span class="comment">//3. 首先获取到即将下载的论文名(鉴于我们上传时 是将论文的文件名存进数据库的)</span></span><br><span class="line">	<span class="keyword">var</span> papername = item.papername  </span><br><span class="line">	</span><br><span class="line">	<span class="comment">//4. 为每一项“模拟按钮” 动态加上类 方便我们后期动态remove掉</span></span><br><span class="line">	<span class="keyword">var</span> download = $(<span class="string">&quot;&lt;a&gt;&lt;/a&gt;&quot;</span>).addClass(papername + <span class="string">&quot; btn btn-danger btn-sm&quot;</span>).append($(<span class="string">&quot;&lt;span&gt;&lt;/span&gt;&quot;</span>).addClass(<span class="string">&quot;glyphicon glyphicon-download&quot;</span>)).append(<span class="string">&quot;下载&quot;</span>).attr(<span class="string">&quot;href&quot;</span>, <span class="string">&quot;/DownloadServlet?paperName=&quot;</span> + item.papername);</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//5. 查询该文件是否可下载</span></span><br><span class="line">	$.ajax(&#123;</span><br><span class="line">		<span class="attr">url</span>: <span class="string">&quot;isExistsFilename&quot;</span>,</span><br><span class="line">		<span class="attr">data</span>: &#123;<span class="string">&quot;paperName&quot;</span>: item.papername&#125;,</span><br><span class="line">		<span class="attr">type</span>: <span class="string">&quot;GET&quot;</span>,</span><br><span class="line">		<span class="attr">dataType</span>: <span class="string">&quot;JSON&quot;</span>,</span><br><span class="line">		<span class="attr">success</span>: <span class="function"><span class="keyword">function</span> (<span class="params">result</span>) </span>&#123;</span><br><span class="line">			<span class="comment">//数据库存储了文件信息的论文 才可以下载</span></span><br><span class="line">			<span class="comment">//如果为false表示不可下载 则根据对应的papername来remove样式</span></span><br><span class="line">			<span class="keyword">if</span>(result.data == <span class="literal">false</span>)&#123;</span><br><span class="line">				$(<span class="string">&quot;.&quot;</span> + item.papername).remove();</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;,</span><br><span class="line">		<span class="attr">error</span>: <span class="function"><span class="keyword">function</span> (<span class="params">error</span>) </span>&#123;</span><br><span class="line">			<span class="built_in">console</span>.log(error)</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></div></figure></li>
</ul>
<p><strong>查询的控制层代码：</strong></p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(value = &quot;/isExistsFilename&quot;, method = RequestMethod.GET)</span></span><br><span class="line">   <span class="meta">@ResponseBody</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> TSMResult <span class="title">isExistsFilename</span><span class="params">(</span></span></span><br><span class="line"><span class="params"><span class="function">           <span class="meta">@RequestParam(&quot;paperName&quot;)</span> String paperName)</span> </span>&#123;</span><br><span class="line">       <span class="comment">//处理中文</span></span><br><span class="line">       Boolean isExists = <span class="keyword">false</span>;</span><br><span class="line">       <span class="keyword">try</span> &#123;</span><br><span class="line">           paperName = <span class="keyword">new</span> String(paperName.getBytes(<span class="string">&quot;ISO-8859-1&quot;</span>), <span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line">           paperName = paperName + <span class="string">&quot;.doc&quot;</span>;</span><br><span class="line">           Source source = adminServer.isExistsPaperFile(paperName);</span><br><span class="line">           isExists = (source == <span class="keyword">null</span>) ? <span class="keyword">false</span> : <span class="keyword">true</span>;</span><br><span class="line">       &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">           System.out.println(<span class="string">&quot;检测到异常！&quot;</span>);</span><br><span class="line">           e.printStackTrace();</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">return</span> <span class="keyword">new</span> TSMResult(<span class="number">200</span>, <span class="string">&quot;查询成功&quot;</span>, isExists);</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></div></figure>

<p>从代码中的三目运算符可以看出：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">isExists = (source == <span class="keyword">null</span>) ? <span class="keyword">false</span> : <span class="keyword">true</span>;</span><br></pre></td></tr></table></div></figure>

<ul>
<li><p>如果这个文件为空则会true，否则为false</p>
</li>
<li><p>也就是说，如果文件不为空，则为false，表示不可下载</p>
</li>
</ul>
<p><strong>服务层代码：</strong></p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> Source <span class="title">isExistsPaperFile</span><span class="params">(String paperName)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> sourceMapper.selectPaperFile(paperName);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></div></figure>

<p><strong>具体Source类看上方代码即可。</strong></p>
<p><strong>Mapper代码：</strong></p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Select(&quot;select* from source where papername = #&#123;paperName&#125;&quot;)</span></span><br><span class="line"><span class="function">Source <span class="title">selectPaperFile</span><span class="params">(<span class="meta">@Param(&quot;paperName&quot;)</span> String paperName)</span></span>;</span><br></pre></td></tr></table></div></figure>


        <h1 id="实现通用JS代码全局引用"   >
          <a href="#实现通用JS代码全局引用" class="heading-link"><i class="fas fa-link"></i></a><a href="#实现通用JS代码全局引用" class="headerlink" title="实现通用JS代码全局引用"></a>实现通用JS代码全局引用</h1>
      
        <h2 id="ElementUI的Message消息提示、Notify通知等等"   >
          <a href="#ElementUI的Message消息提示、Notify通知等等" class="heading-link"><i class="fas fa-link"></i></a><a href="#ElementUI的Message消息提示、Notify通知等等" class="headerlink" title="ElementUI的Message消息提示、Notify通知等等"></a>ElementUI的Message消息提示、Notify通知等等</h2>
      <p><strong>我们希望在弹出消息或者通知（Notify）时</strong></p>
<ul>
<li>根据我们自定义的参数，来相应提示用户</li>
</ul>
<ol>
<li><p>提示用户更改信息成功</p>
</li>
<li><p>这个提示类型是success类型</p>
</li>
</ol>
<p>只需调用：</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">this</span>.$sendMessage(<span class="string">&#x27;这是消息内容&#x27;</span>, <span class="string">&#x27;info/success/error/warning&#x27;</span>)</span><br></pre></td></tr></table></div></figure>

<p><em>info 是灰色 success是绿色 error是红色 warning是黄色</em></p>

        <h3 id="创建通用功能的utils-js"   >
          <a href="#创建通用功能的utils-js" class="heading-link"><i class="fas fa-link"></i></a><a href="#创建通用功能的utils-js" class="headerlink" title="创建通用功能的utils.js"></a>创建通用功能的utils.js</h3>
      <ul>
<li>定义多个function 最后导出即可</li>
</ul>
<p><em>注意，这里有一个知识点：对于默认导出的函数，使用时函数名外不需要{xxx}</em></p>
<p><em>即直接： import XXX from ‘xxx/xxx’</em></p>
<p><strong>而如果有多个函数，想要按需引入的话，需要加上{}</strong></p>
<p>按需引入（包括默认导出函数）：</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; fun1, fun2 &#125; <span class="keyword">from</span> <span class="string">&#x27;xxx/xxx&#x27;</span></span><br></pre></td></tr></table></div></figure>

<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//弹窗发送消息 以传入的参数而定</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">sendMessage</span>(<span class="params">messageContent, messageType</span>) </span>&#123;</span><br><span class="line">	<span class="built_in">this</span>.$message(&#123;</span><br><span class="line">		<span class="attr">message</span>: messageContent,</span><br><span class="line">		<span class="attr">type</span>: messageType,</span><br><span class="line">	&#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">alertTest</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">	alert(<span class="string">&#x27;这是测试方法！&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> sendMessage</span><br></pre></td></tr></table></div></figure>


        <h3 id="main-js中引入并使用"   >
          <a href="#main-js中引入并使用" class="heading-link"><i class="fas fa-link"></i></a><a href="#main-js中引入并使用" class="headerlink" title="main.js中引入并使用"></a>main.js中引入并使用</h3>
      <figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; sendMessage,loadingAnimation &#125; <span class="keyword">from</span> <span class="string">&#x27;./assets/js/utils&#x27;</span></span><br><span class="line">Vue.prototype.$sendMessage = sendMessage </span><br><span class="line">Vue.prototype.$alertTest = alertTest </span><br></pre></td></tr></table></div></figure>

<p>这样我们就可以，直接在vue项目中：使用<code>this.$sendMessage</code>来使用相应方法了。</p>

        <h1 id="HTML中引入VUE并使用ElementUI样式"   >
          <a href="#HTML中引入VUE并使用ElementUI样式" class="heading-link"><i class="fas fa-link"></i></a><a href="#HTML中引入VUE并使用ElementUI样式" class="headerlink" title="HTML中引入VUE并使用ElementUI样式"></a>HTML中引入VUE并使用ElementUI样式</h1>
      
        <h2 id="head标签引入ElementUI的CSS、JS以及VUE的JS"   >
          <a href="#head标签引入ElementUI的CSS、JS以及VUE的JS" class="heading-link"><i class="fas fa-link"></i></a><a href="#head标签引入ElementUI的CSS、JS以及VUE的JS" class="headerlink" title="head标签引入ElementUI的CSS、JS以及VUE的JS"></a>head标签引入ElementUI的CSS、JS以及VUE的JS</h2>
      <figure class="highlight html"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">href</span>=<span class="string">&quot;https://unpkg.com/element-ui/lib/theme-chalk/index.css&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;https://unpkg.com/vue/dist/vue.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;https://unpkg.com/element-ui/lib/index.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="初始VUE（2-0版本）"   >
          <a href="#初始VUE（2-0版本）" class="heading-link"><i class="fas fa-link"></i></a><a href="#初始VUE（2-0版本）" class="headerlink" title="初始VUE（2.0版本）"></a>初始VUE（2.0版本）</h2>
      <ol>
<li>页面非body标签 标记</li>
</ol>
<figure class="highlight html"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span> </span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></div></figure>
<ol start="2">
<li>vue挂载上去（可以<strong>使用template写HTML页面 如果已经有内容了就去掉template</strong>）<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">      <span class="attr">el</span>: <span class="string">&#x27;#app&#x27;</span>,</span><br><span class="line">      <span class="function"><span class="title">data</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">      	<span class="keyword">return</span> &#123;</span><br><span class="line">      			</span><br><span class="line">      	&#125;</span><br><span class="line">      &#125;,</span><br><span class="line">      <span class="attr">methods</span>: &#123;</span><br><span class="line">      	</span><br><span class="line">      &#125;,</span><br><span class="line">      <span class="function"><span class="title">created</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">	  </span><br><span class="line">	  &#125;,</span><br><span class="line">	  <span class="function"><span class="title">mounted</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">			</span><br><span class="line">	  &#125;</span><br><span class="line">    &#125;)</span><br></pre></td></tr></table></div></figure></li>
</ol>
</div><footer class="post-footer"><div class="post-ending ending"><div class="ending__text">------ 本文结束，感谢您的阅读 ------</div></div><div class="post-copyright copyright"><div class="copyright-author"><span class="copyright-author__name">本文作者: </span><span class="copyright-author__value"><a href="https://bruce-jj.gitee.io/blog">何水浪</a></span></div><div class="copyright-link"><span class="copyright-link__name">本文链接: </span><span class="copyright-link__value"><a href="https://bruce-jj.gitee.io/blog/2021/05/12/VUE%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95%E5%8F%8A%E7%9B%B8%E5%BA%94%E5%BA%94%E7%94%A8-4/">https://bruce-jj.gitee.io/blog/2021/05/12/VUE%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95%E5%8F%8A%E7%9B%B8%E5%BA%94%E5%BA%94%E7%94%A8-4/</a></span></div><div class="copyright-notice"><span class="copyright-notice__name">版权声明: </span><span class="copyright-notice__value">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" rel="external nofollow" target="_blank">BY-NC-SA</a> 许可协议。转载请注明出处！</span></div></div><div class="post-tags"><span class="post-tags-item"><span class="post-tags-item__icon"><i class="fas fa-tag"></i></span><a class="post-tags-item__link" href="https://bruce-jj.gitee.io/blog/tags/%E5%89%8D%E7%AB%AF%E6%A1%86%E6%9E%B6%E5%AD%A6%E4%B9%A0/">前端框架学习</a></span></div><nav class="post-paginator paginator"><div class="paginator-prev"><a class="paginator-prev__link" href="/blog/2021/05/15/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E5%AD%A6%E4%B9%A0-%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F/"><span class="paginator-prev__icon"><i class="fas fa-angle-left"></i></span><span class="paginator-prev__text">设计模式学习_策略模式</span></a></div><div class="paginator-next"><a class="paginator-next__link" href="/blog/2021/05/07/hexo%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%BB%A5%E5%8F%8A%E5%B8%B8%E8%A7%81%E9%94%99%E8%AF%AF/"><span class="paginator-prev__text">Hexo学习笔记以及常见错误</span><span class="paginator-next__icon"><i class="fas fa-angle-right"></i></span></a></div></nav></footer></div></div><div class="comments" id="comments"><div id="valine-container"></div></div></div><div class="sidebar-wrap" id="sidebar-wrap"><aside class="sidebar" id="sidebar"><div class="sidebar-nav"><span class="sidebar-nav-toc current">文章目录</span><span class="sidebar-nav-ov">站点概览</span></div><section class="sidebar-toc"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#axios%E8%AF%B7%E6%B1%82-%E5%8C%85%E6%8B%ACGet%E8%AF%B7%E6%B1%82%E4%B8%8EPost%E8%AF%B7%E6%B1%82%EF%BC%88%E6%B3%A8%E6%84%8F%E6%98%AF%E5%BC%82%E6%AD%A5%EF%BC%89"><span class="toc-number">1.</span> <span class="toc-text">
          axios请求 包括Get请求与Post请求（注意是异步）</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Get%E6%96%B9%E6%B3%95"><span class="toc-number">1.1.</span> <span class="toc-text">
          Get方法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AF%B7%E5%8A%A1%E5%BF%85%E6%B3%A8%E6%84%8F%EF%BC%81"><span class="toc-number">1.1.1.</span> <span class="toc-text">
          请务必注意！</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Post%E6%96%B9%E6%B3%95"><span class="toc-number">1.2.</span> <span class="toc-text">
          Post方法</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%85%A8%E5%B1%80%E8%B7%AF%E7%94%B1%E9%85%8D%E7%BD%AE%EF%BC%88%E5%9F%BA%E4%BA%8Esession%E7%9A%84%E5%89%8D%E7%AB%AF%E6%9D%83%E9%99%90%EF%BC%89"><span class="toc-number">2.</span> <span class="toc-text">
          全局路由配置（基于session的前端权限）</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E8%B7%AF%E7%94%B1%EF%BC%8C%E9%99%A4%E4%BA%86%E7%99%BB%E5%BD%95%E9%A1%B5%E9%9D%A2%EF%BC%8C%E5%85%B6%E4%BB%96%E9%83%BD%E8%A6%81%E8%AE%A4%E8%AF%81"><span class="toc-number">2.1.</span> <span class="toc-text">
          配置路由，除了登录页面，其他都要认证</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%99%BB%E5%BD%95%E6%88%90%E5%8A%9F%E6%B7%BB%E5%8A%A0%E5%8F%AF%E2%80%9C%E5%85%81%E8%AE%B8%E9%80%9A%E8%BF%87%E2%80%9D%E7%9A%84%E8%B7%AF%E7%94%B1%E4%BF%A1%E6%81%AF%EF%BC%88Token%EF%BC%8CSession%EF%BC%8CCookies%E5%9D%87%E5%8F%AF%EF%BC%89"><span class="toc-number">2.2.</span> <span class="toc-text">
          登录成功添加可“允许通过”的路由信息（Token，Session，Cookies均可）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%87%E6%BB%A4%E8%B7%AF%E7%94%B1%E6%98%BE%E7%A4%BA%E8%8F%9C%E5%8D%95"><span class="toc-number">2.3.</span> <span class="toc-text">
          过滤路由显示菜单</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Vue-%E9%A1%B9%E7%9B%AE%E4%B8%AD%E5%8A%A0%E5%85%A5%E5%B8%B8%E7%94%A8%E6%8F%92%E4%BB%B6"><span class="toc-number">3.</span> <span class="toc-text">
          Vue 项目中加入常用插件</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8A%A0%E5%85%A5%E6%8F%92%E4%BB%B6"><span class="toc-number">3.1.</span> <span class="toc-text">
          加入插件</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#VUE%E4%B8%AD%E6%B7%BB%E5%8A%A0Echarts%E5%8F%AF%E8%A7%86%E5%8C%96%E5%9B%BE%E8%A1%A8"><span class="toc-number">4.</span> <span class="toc-text">
          VUE中添加Echarts可视化图表</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#npm-%E5%AE%89%E8%A3%85echarts%E6%8F%92%E4%BB%B6"><span class="toc-number">4.1.</span> <span class="toc-text">
          npm 安装echarts插件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%A8%E5%B1%80%E5%BC%95%E5%85%A5ECHARTS"><span class="toc-number">4.2.</span> <span class="toc-text">
          全局引入ECHARTS</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9D%E5%A7%8B%E5%8C%96%E5%9B%BE%E8%A1%A8"><span class="toc-number">4.3.</span> <span class="toc-text">
          初始化图表</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JS%E7%94%BB%E5%9B%BE%E5%8A%A8%E6%80%81%E6%B8%B2%E6%9F%93%EF%BC%88%E6%9F%B1%E7%8A%B6%E5%9B%BE%E4%B8%8E%E9%A5%BC%E7%8A%B6%E5%9B%BE%EF%BC%89"><span class="toc-number">4.4.</span> <span class="toc-text">
          JS画图动态渲染（柱状图与饼状图）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A5%BC%E7%8A%B6%E5%9B%BE"><span class="toc-number">4.4.1.</span> <span class="toc-text">
          饼状图</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%B1%E7%8A%B6%E5%9B%BE"><span class="toc-number">4.4.2.</span> <span class="toc-text">
          柱状图</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E5%9B%BE%E8%A1%A8%E5%8F%82%E6%95%B0"><span class="toc-number">4.4.2.1.</span> <span class="toc-text">
          配置图表参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B8%B2%E6%9F%93%E5%9B%BE%E8%A1%A8"><span class="toc-number">4.4.2.2.</span> <span class="toc-text">
          渲染图表</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#VUEX-%E5%AD%98%E5%80%BC%E4%B8%8E%E5%8F%96%E5%80%BC%E5%8C%BA%E5%9F%9F"><span class="toc-number">5.</span> <span class="toc-text">
          VUEX 存值与取值区域</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A3%B0%E6%98%8Estore%E7%9A%84index-js"><span class="toc-number">5.1.</span> <span class="toc-text">
          声明store的index.js</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9C%A8%E9%9C%80%E8%A6%81%E5%AD%98%E5%80%BC%E3%80%81%E5%8F%96%E5%80%BC%E7%9A%84%E5%9C%B0%E6%96%B9-%E8%B0%83%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-number">5.2.</span> <span class="toc-text">
          在需要存值、取值的地方 调用方法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%96%E5%80%BCgetViewCounter"><span class="toc-number">5.2.1.</span> <span class="toc-text">
          取值getViewCounter</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%98%E5%80%BCsetViewCounter%E6%96%B9%E6%B3%95"><span class="toc-number">5.2.2.</span> <span class="toc-text">
          存值setViewCounter方法</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#WEB%E9%A1%B9%E7%9B%AE%E5%AE%9E%E7%8E%B0%E6%96%87%E4%BB%B6%E4%B8%8B%E8%BD%BD%E4%B8%8E%E4%B8%8A%E4%BC%A0"><span class="toc-number">6.</span> <span class="toc-text">
          WEB项目实现文件下载与上传</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#SSM%E9%A1%B9%E7%9B%AE-%E9%9D%9EMultipartFile%E7%B1%BB"><span class="toc-number">6.1.</span> <span class="toc-text">
          SSM项目+非MultipartFile类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#SpringBoot%E9%A1%B9%E7%9B%AE-JSP-MultipartFile%EF%BC%88%E9%9D%9EAJAX%E5%BC%82%E6%AD%A5%EF%BC%89"><span class="toc-number">6.2.</span> <span class="toc-text">
          SpringBoot项目+JSP+MultipartFile（非AJAX异步）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#SpringBoot-VUE-ElementUI%E5%89%8D%E5%90%8E%E7%AB%AF%E5%88%86%E7%A6%BB"><span class="toc-number">6.3.</span> <span class="toc-text">
          SpringBoot+VUE+ElementUI前后端分离</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AE%9E%E7%8E%B0%E6%96%87%E4%BB%B6%E4%B8%8B%E8%BD%BD"><span class="toc-number">7.</span> <span class="toc-text">
          实现文件下载</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#JSP%E5%AE%9E%E7%8E%B0%E6%96%87%E4%BB%B6%E4%B8%8B%E8%BD%BD%EF%BC%88%E4%B8%94%E5%8A%A8%E6%80%81%E6%A3%80%E6%B5%8B%E6%96%87%E4%BB%B6%E6%98%AF%E5%90%A6%E5%8F%AF%E4%B8%8B%E8%BD%BD%EF%BC%89"><span class="toc-number">7.1.</span> <span class="toc-text">
          JSP实现文件下载（且动态检测文件是否可下载）</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AE%9E%E7%8E%B0%E9%80%9A%E7%94%A8JS%E4%BB%A3%E7%A0%81%E5%85%A8%E5%B1%80%E5%BC%95%E7%94%A8"><span class="toc-number">8.</span> <span class="toc-text">
          实现通用JS代码全局引用</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#ElementUI%E7%9A%84Message%E6%B6%88%E6%81%AF%E6%8F%90%E7%A4%BA%E3%80%81Notify%E9%80%9A%E7%9F%A5%E7%AD%89%E7%AD%89"><span class="toc-number">8.1.</span> <span class="toc-text">
          ElementUI的Message消息提示、Notify通知等等</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E9%80%9A%E7%94%A8%E5%8A%9F%E8%83%BD%E7%9A%84utils-js"><span class="toc-number">8.1.1.</span> <span class="toc-text">
          创建通用功能的utils.js</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#main-js%E4%B8%AD%E5%BC%95%E5%85%A5%E5%B9%B6%E4%BD%BF%E7%94%A8"><span class="toc-number">8.1.2.</span> <span class="toc-text">
          main.js中引入并使用</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#HTML%E4%B8%AD%E5%BC%95%E5%85%A5VUE%E5%B9%B6%E4%BD%BF%E7%94%A8ElementUI%E6%A0%B7%E5%BC%8F"><span class="toc-number">9.</span> <span class="toc-text">
          HTML中引入VUE并使用ElementUI样式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#head%E6%A0%87%E7%AD%BE%E5%BC%95%E5%85%A5ElementUI%E7%9A%84CSS%E3%80%81JS%E4%BB%A5%E5%8F%8AVUE%E7%9A%84JS"><span class="toc-number">9.1.</span> <span class="toc-text">
          head标签引入ElementUI的CSS、JS以及VUE的JS</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9D%E5%A7%8BVUE%EF%BC%882-0%E7%89%88%E6%9C%AC%EF%BC%89"><span class="toc-number">9.2.</span> <span class="toc-text">
          初始VUE（2.0版本）</span></a></li></ol></li></ol></section><!-- ov = overview--><section class="sidebar-ov hide"><div class="sidebar-ov-author"><div class="sidebar-ov-author__avatar"><img class="sidebar-ov-author__avatar_img" src="/blog/images/avatar.jpg" alt="avatar"></div><p class="sidebar-ov-author__text">C'est la vie</p></div><div class="sidebar-ov-state"><a class="sidebar-ov-state-item sidebar-ov-state-item--posts" href="/blog/archives/"><div class="sidebar-ov-state-item__count">48</div><div class="sidebar-ov-state-item__name">归档</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--categories" href="/blog/categories/"><div class="sidebar-ov-state-item__count">19</div><div class="sidebar-ov-state-item__name">分类</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--tags" href="/blog/tags/"><div class="sidebar-ov-state-item__count">20</div><div class="sidebar-ov-state-item__name">标签</div></a></div><div class="sidebar-ov-cc"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" target="_blank" rel="noopener" data-popover="知识共享许可协议" data-popover-pos="up"><img src="/blog/images/cc-by-nc-sa.svg"></a></div></section><div class="sidebar-reading"><div class="sidebar-reading-info"><span class="sidebar-reading-info__text">你已阅读了 </span><span class="sidebar-reading-info__num">0</span><span class="sidebar-reading-info__perc">%</span></div><div class="sidebar-reading-line"></div></div></aside></div><div class="clearfix"></div></div></main><footer class="footer" id="footer"><div class="footer-inner"><div><span>Copyright © 2021</span><span class="footer__icon"><i class="fas fa-heart"></i></span><span>何水浪</span></div><div><span>由 <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a> 强力驱动</span><span> v5.4.0</span><span class="footer__devider">|</span><span>主题 - <a href="https://github.com/liuyib/hexo-theme-stun/" title="Stun" target="_blank" rel="noopener">Stun</a></span><span> v2.6.2</span></div></div></footer><div class="loading-bar" id="loading-bar"><div class="loading-bar__progress"></div></div><div class="back2top" id="back2top"><span class="back2top__icon"><i class="fas fa-rocket"></i></span></div></div><div class="search-mask"></div><div class="search-popup"><span class="search-close"></span><div class="search-input"><input placeholder="搜索文章（支持多关键词，请用空格分隔）"></div><div class="search-results"></div></div><script src="https://cdn.jsdelivr.net/npm/jquery@v3.4.1/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.ui.min.js"></script><script>function initSearch() {
  var isXML = true;
  var search_path = 'search.json';

  if (!search_path) {
    search_path = 'search.xml';
  } else if (/json$/i.test(search_path)) {
    isXML = false;
  }

  var path = '/blog/' + search_path;
  $.ajax({
    url: path,
    dataType: isXML ? 'xml' : 'json',
    async: true,
    success: function (res) {
      var datas = isXML ? $('entry', res).map(function () {
        // 将 XML 转为 JSON
        return {
          title: $('title', this).text(),
          content: $('content', this).text(),
          url: $('url', this).text()
        };
      }).get() : res;
      var $input = $('.search-input input');
      var $result = $('.search-results');
      // 搜索对象（标题、内容）的权重，影响显示顺序
      var WEIGHT = { title: 100, content: 1 };
      var searchPost = function () {
        var searchText = $input.val().toLowerCase().trim();
        // 根据空白字符分隔关键字
        var keywords = searchText.split(/[\s]+/);
        // 搜索结果
        var matchPosts = [];

        // 有多个关键字时，将原文字整个保存下来
        if (keywords.length > 1) {
          keywords.push(searchText);
        }
        // 防止未输入字符时搜索
        if (searchText.length > 0) {
          datas.forEach(function (data) {
            var isMatch  = false;
            // 没有标题的文章使用预设的 i18n 变量代替
            var title = (data.title && data.title.trim()) || '[ 文章无标题 ]';
            var titleLower = title && title.toLowerCase();
            // 删除 HTML 标签 和 所有空白字符
            var content = data.content && data.content.replace(/<[^>]+>/g, '');
            var contentLower = content && content.toLowerCase();
            // 删除重复的 /
            var postURL = data.url && decodeURI(data.url).replace(/\/{2,}/g, '/');
            // 标题中匹配到的关键词
            var titleHitSlice = [];
            // 内容中匹配到的关键词
            var contentHitSlice = [];

            keywords.forEach(function (keyword) {
              /**
              * 获取匹配的关键词的索引
              * @param {String} keyword 要匹配的关键字
              * @param {String} text 原文字
              * @param {Boolean} caseSensitive 是否区分大小写
              * @param {Number} weight 匹配对象的权重。权重大的优先显示
              * @return {Array}
              */
              function getIndexByword (word, text, caseSensitive, weight) {
                if (!word || !text) {
                  return [];
                };

                var startIndex = 0; // 每次匹配的开始索引
                var index = -1;     // 匹配到的索引值
                var result = [];    // 匹配结果

                if (!caseSensitive) {
                  word = word.toLowerCase();
                  text = text.toLowerCase();
                }

                while((index = text.indexOf(word, startIndex)) !== -1) {
                  var hasMatch = false;
                  // 索引位置相同的关键词，保留长度较长的
                  titleHitSlice.forEach(function (hit) {
                    if (hit.index === index && hit.word.length < word.length) {
                      hit.word = word;
                      hasMatch = true;
                    }
                  });
                  startIndex = index + word.length;
                  !hasMatch && result.push({ index: index, word: word, weight: weight });
                }
                return result;
              }
              titleHitSlice = titleHitSlice.concat(getIndexByword(keyword, titleLower, false, WEIGHT.title));
              contentHitSlice = contentHitSlice.concat(getIndexByword(keyword, contentLower, false, WEIGHT.content));
            });

            var hitTitle = titleHitSlice.length;
            var hitContent = contentHitSlice.length;

            if (hitTitle > 0 || hitContent > 0) {
              isMatch = true;
            }
            if (isMatch) {
              ;[titleHitSlice, contentHitSlice].forEach(function (hit) {
                // 按照匹配文字的索引的递增顺序排序
                hit.sort(function (left, right) {
                  return left.index - right.index;
                });
              });
              /**
              * 给文本中匹配到的关键词添加标记，从而进行高亮显示
              * @param {String} text 原文本
              * @param {Array} hitSlice 匹配项的索引信息
              * @param {Number} start 开始索引
              * @param {Number} end 结束索引
              * @return {String}
              */
              function highlightKeyword (text, hitSlice, start, end) {
                if (!text || !hitSlice || !hitSlice.length) {
                  return;
                }

                var result = '';
                var startIndex = start;
                var endIndex = end;
                hitSlice.forEach(function (hit) {
                  if (hit.index < startIndex) {
                    return;
                  }

                  var hitWordEnd = hit.index + hit.word.length;
                  result += text.slice(startIndex, hit.index);
                  result += '<b>' + text.slice(hit.index, hitWordEnd) + '</b>';
                  startIndex = hitWordEnd;
                });
                result += text.slice(startIndex, endIndex);
                return result;
              }

              var postData = {};
              // 文章总的搜索权重
              var postWeight = titleHitSlice.length * WEIGHT.title + contentHitSlice.length * WEIGHT.content;
              // 标记匹配关键词后的标题
              var postTitle = highlightKeyword(title, titleHitSlice, 0, title.length) || title;
              // 标记匹配关键词后的内容
              var postContent;
              // 显示内容的长度
              var SHOW_WORD_LENGTH = 200;
              // 命中关键词前的字符显示长度
              var SHOW_WORD_FRONT_LENGTH = 20;
              var SHOW_WORD_END_LENGTH = SHOW_WORD_LENGTH - SHOW_WORD_FRONT_LENGTH;

              // 截取匹配的第一个字符，前后共 200 个字符来显示
              if (contentHitSlice.length > 0) {
                var firstIndex = contentHitSlice[0].index;
                var start = firstIndex > SHOW_WORD_FRONT_LENGTH ? firstIndex - SHOW_WORD_FRONT_LENGTH : 0;
                var end = firstIndex + SHOW_WORD_END_LENGTH;
                postContent = highlightKeyword(content, contentHitSlice, start, end);
              } else { // 未匹配到内容，直接截取前 200 个字符来显示
                postContent = content.slice(0, SHOW_WORD_LENGTH);
              }
              postData.title = postTitle;
              postData.content = postContent;
              postData.url = postURL;
              postData.weight = postWeight;
              matchPosts.push(postData);
            }
          });
        }

        var resultInnerHtml = '';
        if (matchPosts.length) {
          // 按权重递增的顺序排序，使权重大的优先显示
          matchPosts.sort(function (left, right) {
            return right.weight - left.weight;
          });
          resultInnerHtml += '<ul>';
          matchPosts.forEach(function (post) {
            resultInnerHtml += '<li><a class="search-results-title" href="' + post.url + '">';
            resultInnerHtml += post.title;
            resultInnerHtml += '</a><div class="search-results-content">';
            resultInnerHtml += post.content;
            resultInnerHtml += '</div></li>';
          });
          resultInnerHtml += '</ul>';
        } else {
          resultInnerHtml += '<div class="search-results-none"><i class="far fa-meh"></i></div>';
        }
        $result.html(resultInnerHtml);
      };
      $input.on('input', searchPost);
      $input.on('keyup', function (e) {
        if (e.keyCode === Stun.utils.codeToKeyCode('Enter')) {
          searchPost();
        }
      });
    }
  });
}

function closeSearch () {
  $('body').css({ overflow: 'auto' });
  $('.search-popup').css({ display: 'none' });
  $('.search-mask').css({ display: 'none' });
}

window.addEventListener('DOMContentLoaded', function () {
  Stun.utils.pjaxReloadLocalSearch = function () {
    $('.header-nav-search').on('click', function (e) {
      e.stopPropagation();
      $('body').css('overflow', 'hidden');
      $('.search-popup')
        .velocity('stop')
        .velocity('transition.expandIn', {
          duration: 300,
          complete: function () {
            $('.search-popup input').focus();
          }
        });
      $('.search-mask')
        .velocity('stop')
        .velocity('transition.fadeIn', {
          duration: 300
        });

      initSearch();
    });
    $('.search-mask, .search-close').on('click', function () {
      closeSearch();
    });
    $(document).on('keydown', function (e) {
      // Escape <=> 27
      if (e.keyCode === Stun.utils.codeToKeyCode('Escape')) {
        closeSearch();
      }
    });
  };

  Stun.utils.pjaxReloadLocalSearch();
}, false);

function safeOpenUrl(url) {
  var newTab = window.open();
  newTab.opener = null;
  newTab.location = url;
}

function extSearch(engine) {
  var engines = {
    google: 'https://www.google.com/search?q=',
    bing: 'https://cn.bing.com/search?q=',
    baidu: 'https://www.baidu.com/s?ie=UTF-8&wd=',
  };
  var host = window.location.host;
  var query = $('.search-input input').val().toLowerCase().trim();
  var uri = engines[engine] + query + ' site:' + host;

  if (query) {
    safeOpenUrl(uri);
  } else {
    Stun.utils.popAlert('warning', '请输入字符');
  }
}

var assistSearchList = window.CONFIG.assistSearch;

if (Array.isArray(assistSearchList)) {
  assistSearchList.forEach(function (name) {
    document.querySelector('.search-btns-item--' + name).addEventListener('click', function () {
      extSearch(name);
    }, false);
  });
}</script><script src="https://cdn.jsdelivr.net/npm/leancloud-storage@latest/dist/av-min.js"></script><script src="https://cdn.jsdelivr.net/npm/valine@latest/dist/Valine.min.js"></script><script>function loadValine () {
  var GUEST_INFO = ['nick', 'mail', 'link'];
  var guest_info = 'nick';

  guest_info = guest_info.split(',').filter(function(item) {
    return GUEST_INFO.indexOf(item) > -1;
  });
  new Valine({
    el: '#valine-container',
    appId: 'fJ4fFnieffDVKy4Jkn16aHdO-gzGzoHsz',
    appKey: 'qfVo2CUC2pJ3udFzLFnd0GBM',
    notify: true,
    verify: true,
    placeholder: '说点啥再走呗~爱你',
    avatar: 'mp',
    meta: guest_info,
    pageSize: '10' || 10,
    visitor: false,
    recordIP: false,
    lang: '' || 'zh-cn',
    path: window.location.pathname
  });
}

if (false) {
  loadValine();
} else {
  window.addEventListener('DOMContentLoaded', loadValine, false);
}</script><script src="/blog/js/utils.js?v=2.6.2"></script><script src="/blog/js/stun-boot.js?v=2.6.2"></script><script src="/blog/js/scroll.js?v=2.6.2"></script><script src="/blog/js/header.js?v=2.6.2"></script><script src="/blog/js/sidebar.js?v=2.6.2"></script><script type="application/json" src="/blog/search.json"></script></body></html>